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.

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.

  1. Take Octal number as input
  2. Convert each digit of octal into binary.
  3. That will be output as a binary number.
Example
Input:
12

CONVERSION: 
(12)8 = (1010)2

Output:
1010

Helpful topics to understand this program better are-


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.

  1. Initially, the program will prompt the user to enter an octal number and store it in int octal.
  2. Then the function convertOctaltoBinary(int octal) is invoked and inputs are passed in the function and using while loop calculations are performed.
  3. In this method we will first convert the Octal to Decimal and then convert Decimal to Octal.
  4. 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.

Output
Enter 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!! 😊

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x