Java Program to Convert Octal to Decimal Number

Here is 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;

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

Recommended Posts
Java Program to Convert Decimal to Binary Numbers
Java program to convert binary number to decimal number
Java Program to Convert Fahrenheit to Celsius
Java Program to Convert Celsius to Fahrenheit
Java Program to find Area Of Equilateral Triangle
Java Program to Calculate Volume of Sphere
Java Program to Find Surface Area and Volume of Cylinder
Java Program to Check If a Year is Leap Year or Not
Java Program to Calculate Simple Interest
Java Program to Calculate Compound Interest
Java Program to Find All Factors of a Number
All Java Programs