Java Program to Convert Octal Number to Decimal and vice-versa

   
Java Program to Convert Octal Number to Decimal and vice-versa

What is an Octal Number?

In Java, an octal number is a number represented in the base-8 number system, which uses digits from 0 to 7. To denote an octal number in Java, the number is preceded by a 0 (zero) prefix. For example, the octal number 27 in Java would be represented as 033, where the prefix 0 indicates that the number is in octal format.

 

Steps to Convert Octal number into Decimal :

  1. Write down the octal number you want to convert.
  2. Starting from the rightmost digit, assign a power of 8 to each digit of the octal number, starting with 8^0, then 8^1, then 8^2, and so on, increasing the exponent by 1 for each digit.
  3. Multiply each digit of the octal number by its corresponding power of 8.
  4. Add the results of the multiplication from step 3 to get the decimal equivalent of the octal number.

Example : Convert the Octal Number 263 to Decimal.

  1. The octal number is 263.
  2. Assign a power of 8 to each digit, starting from the rightmost digit:
    3 × 8^0 = 3
    6 × 8^1 = 48
    2 × 8^2 = 128
  3. Multiply each digit by its corresponding power of 8:
    3 × 1 = 3
    6 × 8 = 48
    2 × 64 = 128
  4. Add the results of the multiplication:
    3 + 48 + 128 = 179
    Therefore, the decimal equivalent of the octal number 263 is 179.

Example 1 : Convert Octal Number into Decimal .

Run

public class Main {
    public static void main(String[] args) {
        // define the octal number as a string
        String octalStr = "345";

        // convert the octal number to decimal
        int decimalNum = 0;
        int power = 0;
        for (int i = octalStr.length() - 1; i >= 0; i--) {
            int digit = octalStr.charAt(i) - '0';
            decimalNum += digit * Math.pow(8, power);
            power++;
        }

        // display the decimal equivalent of the octal number
        System.out.println("The decimal equivalent of octal number " + octalStr + " is " + decimalNum);
    }
}

Output :

The decimal equivalent of octal number 345 is 229

Steps to convert a decimal number to an octal number :

  1. Divide the decimal number by 8.
  2. Write down the remainder from step 1 as the least significant digit of the octal number.
  3. If the quotient from step 1 is greater than 0, repeat steps 1 and 2 using the quotient as the new decimal number.
  4. Write the digits obtained from step 2 in reverse order to obtain the octal number.

Example : Convert the decimal number 85 to an octal number.

  1. 85 divided by 8 is 10, with a remainder of 5.
  2. The least significant digit of the octal number is 5.
  3. 10 divided by 8 is 1, with a remainder of 2.
  4. The next digit of the octal number is 2.
  5. 1 divided by 8 is 0, with a remainder of 1.
  6. The most significant digit of the octal number is 1.
  7. Writing down the digits obtained in reverse order, we obtain the octal number 125.

So, the decimal number 85 is equivalent to the octal number 125.

Example 2 : Convert a decimal number to an octal number.

Run

public class Main {
    public static void main(String[] args) {
        // define the decimal number
        int decimalNum = 85;

        // convert the decimal number to octal
        int octalNum = 0;
        int power = 0;
        while (decimalNum > 0) {
            int digit = decimalNum % 8;
            octalNum += digit * Math.pow(10, power);
            decimalNum /= 8;
            power++;
        }

        // display the octal equivalent of the decimal number
        System.out.println("The octal equivalent of decimal number is " + octalNum);
    }
}

Output :

The octal equivalent of decimal number is 125

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription