Java Program to Convert Octal to Decimal Number

  • Write a Java program to convert octal number to decimal number system.

In this java program, we will take an octal number from user and then convert it to decimal number system.
For Example,
144 in Octal is equivalent to 100 in Decimal number system.

  • Decimal number system is base 10 number system and using digits from 0 to 9.
  • Octal number system is a base 8 number system using digits 0 and 7.
Algorithm to convert Octal to Decimal number
We multiply each octal digit with 8i and add them, where i is the position of the octal digit(starting from 0) from right side. Least significant digit is at position 0.

Let's convert 144(octal number) to decimal number
Decimal number = 1*82 + 4*81 + 4*80 = 64 + 32 + 4 = 100

Java program to convert octal number to decimal number

package com.tcc.java.programs;

import java.util.Scanner;

/**
 * Java Program to convert Octal number to Decimal Number
 */
public class OctalToDecimal {
    public static void main(String args[]) {
        int oct, dec = 0, i = 0, temp;
        Scanner scanner = new Scanner(System.in);
        // Take an Octal Number as input from user
        System.out.println("Enter an Octal Number");
        oct = scanner.nextInt();
        // Create a copy of oct number
        temp = oct;

        // Convert Octal number to decimal number
        while (temp != 0) {
            dec = dec + (temp % 10) * (int) Math.pow(8, i);
            i++;
            temp = temp / 10;
        }

        System.out.println("Decimal Value : " + dec);
    }
}
Output
Enter an Octal Number
100
Decimal Value : 64
Enter an Octal Number
10
Decimal Value : 8