# Java Program to Convert Octal to Decimal

In Computer Science, generally Octal number system used to store big data values. In the previous post, we had developed a Java program to convert decimal to octal value. Now in this post, we will develop a Java program to convert the octal value to decimal value.

In Octal number system 8 and 9 are not available because their base is 8.

Procedure to develop a method to convert octal to decimal,

1) Take an octal number
2) Declare variables decimal, remainder, multiplier, and iteration variable

``````// store decimal value
int decimal = 0;

// store reminder in every iteration
int remainder = 0;

// calculate multiplier value
int multiplier = 0;

// iteration variable
int i = 0; ``````

3) Calculate the remainder after dividing by base of decimal that is 10
4) Calculate the multiplier value. In each iteration `multiplier = 8^i`
5) Increase decimal value as, `decimal = decimal + (remainder * multiplier)`
6) Update octal number through dividing it by 10
7) Increase the iteration variable by 1
8) Repeat 3 to 7 steps until the octal number becomes 0

``````import java.util.Scanner;

public class NumberConversion {
private static int toDecimal(int octal) {

// declare variables
int decimal = 0;
int remainder = 0;
int multiplier = 0;
int i = 0;

while (octal!=0) {
// find remainder after
// dividing by 8
remainder = octal % 10;

// calculate multiplier value
multiplier = (int) Math.pow(8, i);
// increase decimal value
decimal += (remainder * multiplier);

// divide decimal value by 8
octal /= 10;
// increase i value by 1
i++;
}

return decimal;
}

public static void main(String[] args) {

// declare variables
int decimal = 0;
int octal = 0;

// create Scanner class object
Scanner scan = new Scanner(System.in);

System.out.print("Enter octal number::");
octal = scan.nextInt();

// convert decimal to decimal
decimal = toDecimal(octal);

// display result
System.out.println("Decimal value = "
+ decimal);

// close Scanner class object
scan.close();
}
}``````

The output for different test-cases:-

Enter octal number:: 12
Decimal value = 10

Enter octal number:: 15
Decimal value = 13

In this program, we have used a while loop to find the sum of natural numbers in Java. While loop is a pre-test loop where the expression is evaluated then only statements are executed. It uses a test expression to control the loop. Before every iteration of the loop, the test expression is evaluated.

## Java Program to Convert Octal to Decimal using for loop

We can also use for loop instead of using a while loop. The for loop is also a pre-test loop, where first of all initialization expression is evaluated then the condition is checked and if the condition is true then only the statements of the for loop are executed.

``````import java.util.Scanner;

public class NumberConversion {

private static int toDecimal(int octal) {
// declare variables
int decimal = 0;
for(int i=0; octal!=0; octal/=10, i++) {
decimal += ( (octal%10) *
((int)Math.pow(8, i)));
}
return decimal;
}

public static void main(String[] args) {

// declare variables
int decimal = 0;
int octal = 0;

// create Scanner class object
Scanner scan = new Scanner(System.in);

System.out.print("Enter octal number:");
octal = scan.nextInt();

// convert decimal to decimal
decimal = toDecimal(octal);

// display result
System.out.println("Decimal value = "
+ decimal);

// close Scanner class object
scan.close();
}
}``````

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!