# C Program to Convert Octal to Decimal

|In this C Programming example, we will implement the program to convert a number from octal to decimal and print the output on the console.

Table of Contents

## 1. How do you Convert a number from Octal to Decimal?

The **octal numeral system** is the **base-8** number system and accepts the digits from 0 to 7. **Octal** numerals can be formed using binary numerals and grouping the consecutive binary digits into groups of three (*starting from the right*).

- The octal to decimal conversion process is quite similar to that of binary to decimal conversion.
- Here, instead of
**base 2**, we are going to use**base 8.** - So, to begin the conversion we have to start multiplying the digits of the number from the right-hand side with increasing powers of 8.
- Starting from 0 and then adding up all the products.

ExampleInput:123CONVERSION:(123)_{8}= (83)_{10}(123 % 10) * 8^{0}= 3 * 1 =3(12 % 10) * 8^{1}= 2 * 8 =16(1 % 10) * 8^{2}= 1 * 64 =64Output:64 + 16 + 3 =83

Helpful topics to understand this program better are-

- Data Types in C
- Console Input / Output in C
- Functions in C
- Recursive functions in C
- C program to convert decimal to octal

## 2. C Program to Convert Octal to Decimal

Letβs examine the execution(kind of pseudocode) of the program to convert a number from octal to decimal in C.

- Initially, the program will prompt the user to enter a decimal number and store the input value in
`int decimalNum`

. - Now, we invoke the function
`convertDecimalToOctal(int decimalNum)`

and pass the user input decimal number to it. - Later the calculations are performed on the input using a while loop.
- Print the final output to the console.

Let us now implement the above execution of the program *to convert a number from octal to decimal in C.*

#include <stdio.h> #include <math.h> long convertOctalToDecimal(int octalNum); long convertOctalToDecimal(int octalNum){ int decimalNum = 0, i = 0; while(octalNum != 0){ decimalNum += (octalNum%10) * pow(8,i); ++i; octalNum/=10; } i = 1; return decimalNum; } int main(){ int octalNum; printf("Enter an octal number: "); scanf("%d", &octalNum); printf("%d in octal is %ld in decimal", octalNum, convertOctalToDecimal(octalNum)); return 0; }

**Note:** In the above program we have used int data type to store the input. However, we can use float, double, long datatype with their format specifier as well.

OutputEnter an octal number: 45 45 in octal is 37 in decimal

## 3. Conclusion

In this C Programming example, we have discussed *how to convert a number from octal to decimal* *in C*.

**Helpful Links**

Please follow **C Programming tutorials** or the **menu in the sidebar** for the complete tutorial series.

Also for the example C programs please refer to **C Programming Examples**.

All examples are hosted on **Github**.

**Recommended Books**

**An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding**

**Do not forget to share and Subscribe.**

* Happy coding!! *π