In the previous article, we have discussed Java Program for Binary to Hexadecimal
In this article we will discuss about how to convert Octal to Decimal.
Program To Convert Octal To Decimal
Before jumping into the program directly, let’s first know about octal and decimal.
Decimal Number :
- The number system with base 10 is generally called decimal number system .
- This number system us usually consists of 10 digits i.e. 0,1,2,3,4,5,6,7,8,9
- This is the popular number system which is used in daily life .
- Example – (156)10 where “10” represents the base and “156” represents the decimal number.
Octal Number :
- The number system with base 8 is generally called octal number system .
- This number system us usually consists of 8 digits i.e. 0,1,2,3,4,5,6,7
- Example – (156)8 where “8” represents the base and “156” represents the octal
- But (186)8 will be a wrong representation because the digits are possible between 0 to 7
Now while converting to the octal to decimal
As the base is 8 so We need to multiply the digits with the base value with the power of 8 .
For example : lets take a octal number system (55)8 we need to convert with the equivalent decimal value . so ,
(55)8 = (5 × 8¹) + (5 × 8⁰)
= 40+5
= 45
Which can be represented as (45)10
Let’s see different ways to convert Octal to Decimal.
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.
Method 1 : Java Program for Octal to Decimal Using Integer.parseInt() method
Approach :
- Take a octal value form the input .
- Convert it into its decimal value by using
Integer.parseInt(input value , 8)
; - Store it into a variable .
- Print the result .
Program :
Let’s see the program to understand it more clearly.
import java.util.Scanner; public class Main { public static void main(String[] args) { // creating scanner object Scanner sc = new Scanner(System.in); // input an octal value as a string through scanner class System.out.println("Enter a octal Value : "); String input1=sc.next(); // converting to decimal int output =Integer.parseInt(input1, 8); System.out.println("Converted integer is : "+output); } }
Output : Enter an octal Value : 55 Converted integer is :45
Method 2 : Java Program for Octal to Decimal By using mathematical approach
Approach :
- Take a octal value form the input .
- Take a for loop to iterate each digits of octal value and multiply with the power of 8 according to there position .
- Each time store it into a intermediate variable .
- After successful termination of loop Print the result .
Program :
Let’s see the program to understand it more clearly.
import java.util.Scanner; public class Main { public static void main(String[] args) { // creating scanner object Scanner sc = new Scanner(System.in); // input an octal value as a string through scanner class System.out.println("Enter a octal Value : "); int input1=sc.nextInt(); int x = input1; int output=0; // converting to Integer for (int a = 0; x > 0; a++) { // Taking the last digit int temp = x % 10; // power on 8 suitable to its position. double p = Math.pow(8, a); // Multiplying the digits to the Input and then adding it to result output += (temp * p); x = x / 10; } System.out.println("Converted integer is :"+output); } }
Output : Enter an octal Value : 55 Converted integer is :45
Want to excel in java coding? Practice with these Java Programs examples with output and write any
kind of easy or difficult programs in the java language.
Related Java Programs: