Write a Java program to Convert Octal to Decimal.
Octal Number: In Java, an octal number is a numeric value represented in base-8, using digits from 0 to 7 that is 0, 1, 2, 3, 4, 5, 6, 7. It is denoted by a leading 0 followed by octal digits. Octal numbers can be used in Java to represent values or perform calculations in a base-8 system.
Decimal Number: The decimal number system is one of the most popular number system used in day to day life. It is also known as base 10 number system as it uses digits from 0 to 9
that is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. In order to represent number system, we need a base. Decimal numbers are represented in the base 10. Base 10 is the most common base in the world.
To solve the problem, we create a class with two methods. The first method handles taking input from the user, specifically in the octal format. The second method is responsible for converting the user’s input from octal to decimal. We can access these methods using an object of the class.
Here is the source code of the Java Program to Convert Octal to Decimal. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
* Octal to Decimal Conversion in Java
System.out.println("Octal to Decimal");
scan = new Scanner(System.in);
System.out.println("\nEnter the number :");
num = Integer.parseInt(scan.nextLine(), 8);
String decimal = Integer.toString(num);
System.out.println("Decimal Value is : " + decimal);
public static void main(String args)
Octal_Decimal obj = new Octal_Decimal();
1. The program defines a class named Octal_Decimal, which handles the conversion from octal to decimal.
2. Inside the Octal_Decimal class, there is a Scanner object called ‘scan‘ and an integer variable named ‘num‘.
3. The ‘getVal()‘ method is responsible for taking user input. It prompts the user to enter an octal number and reads it using the Scanner object. The input is then parsed into an integer using Integer.parseInt() with a radix of 8.
4. The ‘convert()‘ method converts the parsed octal number, stored in ‘num‘, into a decimal string using Integer.toString().
5. In the main() method, an object of the Octal_Decimal class is created called ‘obj‘.
6. The ‘getVal()‘ method is called on ‘obj‘ to get the octal input from the user.
7. The ‘convert()‘ method is called on ‘obj‘ to perform the conversion from octal to decimal.
8. The resulting decimal value is displayed to the user.
$ javac MainClass.java $ java MainClass Octal to Decimal Enter the number : 10 Decimal Value is : 8