**Octal to Decimal: How to Convert**

Octal to decimal conversion is one of the most commonly taught problem solving exercises in computer basics. So is there an octal to decimal formula? Yes, an octal number can be converted to a decimal number using the following formula:

Decimal Form = Ʃ(ai x 8i)

In this formula, ‘a’ is the individual digit being converted, while ‘i‘ is the number of the digit counting from the right-most digit in the number, the right-most digit starting with 0.

For example:

Convert (765)8 into decimal:

(765)8 = (7 x 82) + (6 x 81) + (5 x 80)

= (7 x 64) + (6 x 8 ) + (5 x 1)

= 448 + 48 + 5

= 501

Hence (765)8 = (501)10

Similarly,

(1336)8 = (734)10

(5467)8 = (2871)10

(6345)8 = (3301)10(

(76534)8 = (32092)10

(724635)8 = (240029)10

Try out yourselves!

**How the Program works?**

This program accepts the octal number from the user, saves individual digits to individual spaces in the array in the 1st ‘for’ loop and applies the above formula on them in the second ‘for’ loop.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//Variables: //int num:To store the octal number entered by the user. //int DecNum:To store the converted Decimal number //int i, j : To keep the counter for the array //int power: To initialize the power to 0 and increment by 1 for every next digit import java.util.Scanner; public class OctalToDecimal{ static Scanner MyScan = new Scanner(System.in); public static void main(String args[]) { int arr[] = new int[20]; int DecNum=0,i=0; System.out.println("Enter the octal number: "); int num = MyScan.nextInt(); for(i=0; num>0; i++) //Save the individual digits of the Octal number to the array. { arr[i] = num % 10; num = num / 10; } for(int power=0, j=0; j<i ; j++,power++) { DecNum = (int) (DecNum + arr[j] * Math.pow(8,power)); } System.out.println("Decimal number: "+ DecNum); } } |

Output:

Tested in Eclipse.

#### Vlad

#### Latest posts by Vlad (see all)

- Code jam “Tic-Tac-Toe-Tomek” solution in java - April 19, 2013
- Code jam “Minimum Scalar product” solution in java - March 18, 2013
- Code jam Store Credit solution in java - March 10, 2013