# Java Program to Convert Octal to Decimal

Problem Description

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.

Problem Solution

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.

1. `/*`
2. ` * Octal to Decimal Conversion in Java`
3. ` */`
4. ` `
5. `import java.util.Scanner;`
6. ` class Octal_Decimal `
7. ` {`
8. `    Scanner scan;`
9. `    int num;`
10. `    void getVal() `
11. `     {`
12. `                 System.out.println("Octal to Decimal");`
13. `	scan = new Scanner(System.in);`
14. `	System.out.println("\nEnter the number :");`
15. `	num = Integer.parseInt(scan.nextLine(), 8);`
16. `      }`
17. `    void convert() `
18. `       {`
19. `	String decimal = Integer.toString(num);`
20. `                 System.out.println("Decimal Value is : " + decimal);`
21. `       }`
22. `}`
23. `class MainClass `
24. `{`
25. `    public static void main(String args[]) `
26. `      {`
27. `         Octal_Decimal obj = new Octal_Decimal();`
28. `         obj.getVal();`
29. `         obj.convert();`
30. `      }`
31. `}`
Program Explanation

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.

Program Output
```\$ javac MainClass.java
\$ java MainClass

Octal to Decimal

Enter the number :
10
Decimal Value is : 8```

To practice programs on every topic in Java, please visit “Programming Examples in Java”, “Data Structures in Java” and “Algorithms in Java”.

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]