# C Program to Convert Octal to Binary

|In this C Programming example, we will implement the program to convert a number from octal to binary using Decimal as an intermediate state and print the output on the console.

Table of Contents

## 1. How do you convert a number from Octal to Binary?

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*).

**Binary** is the simplest kind of number system that accepts only two digits of 0 and 1 (i.e. the value of base 2).

This method is simple and also works as the reverse of Binary to Octal Conversion. The algorithm is explained below.

- Take Octal number as input
- Convert each digit of octal into binary.
- That will be output as a binary number.

ExampleInput:12CONVERSION:(12)_{8}= (1010)_{2}Output: 1010

Helpful topics to understand this program better are-

- Data Types in C
- Console Input / Output in C
- Functions in C
- Recursive functions in C
- For and While Loop in C

## 2. C Program to Convert Octal to Binary

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

- Initially, the program will prompt the user to enter an octal number and store it in
`int octal`

. - Then the function
`convertOctaltoBinary(int octal)`

is invoked and inputs are passed in the function and using**while loop**calculations are performed. - In this method we will first convert the Octal to Decimal and then convert Decimal to Octal.
- Print the final result to console.

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

#include <math.h> #include <stdio.h> long convertOctaltoBinary(int octal); long convertOctaltoDecimal(int octal) { int decimal = 0, i = 0; while (octal != 0) { decimal += (octal % 10) * pow(8, i); ++i; octal /= 10; } return decimal; } long convertOctaltoBinary(int octal) { int i = 1; long binary = 0, decimal = convertOctaltoDecimal(octal); while (decimal != 0) { binary += (decimal % 2) * i; decimal /= 2; i *= 10; } return binary; } int main() { int octal; printf("Enter an octal number: "); scanf("%d", &octal); printf("%d in octal is %ld in binary", octal, convertOctaltoBinary(octal)); 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: 16 16 in octal is 1110 in binary

## 3. Conclusion

In this C Programming example, we have discussed *how to convert a number from Octal to Binary using Decimal as an intermediate state 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!! *😊