**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* = **Ʃ(a _{i} x 8^{i})**

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 8^{2}) + (6 x 8^{1}) + (5 x 8^{0})

= (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 27 28 29 30 31 32 33 34 35 36 37 |
//Variables: //int OctNum: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 #include<stdio.h> #include<conio.h> #include<math.h> int main() { int i,power,j,OctNum,arr[20]; double DecNum = 0; printf("Octal Number to Decimal Number converter.\n\n"); printf("Enter the Number: "); scanf_s("%d", &OctNum); for(i=0, j=0; OctNum>0; i++) //Save the individual digits of the Octal number to the array. { arr[i] = OctNum % 10; OctNum = OctNum / 10; } //first digit(in ones place) x (8 raise to 0)+2nd digit(tens) x (8 raise to 1) +...+last digit x (8 raise to n-1) where 'n' is the number of digits for(power=0, j=0; j<i ; j++,power++) { DecNum = DecNum + (arr[j] * pow(8.0,power)); } printf("\nThe Decimal form is %.0f", DecNum); getch(); return 0; } |

Note:’%.0f’ tells our compiler to only print 0 numbers after the decimal

Tested in: Visual Studio 2010/Visual C++

#### Lalit Mali

#### Latest posts by Lalit Mali (see all)

- Rank Pages in a Directory by Occurrence of a Particular Word in them – Java Data Mining - October 16, 2012
- Java Data Mining: Number of occurrences of a word in a file - October 15, 2012
- Calculate factorial of a number using C++ Recursive function - October 14, 2012

## Leave a Reply