Octal to Binary and Binary to Octal Program in C

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest in 3 Numbers
Find Grade of student
Find the absolute value
Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

In Computer Science, generally Octal number system used to store big data values. We know that computer only understands binary numbers that are 0 and 1. Every data are given as input to computer converts into binary number system. Here we will write a C program to convert octal to the binary number system. In the same way, conversion of Binary to Decimal and Decimal to Binary, Decimal to Octal and Octal to Decimal, and Binary to Octal also can be done.

For convert from Octal to Binary, we need to calculate power. So, we will use pow() function defined under standard library function math.h. First Octal will be converted to Decimal and after that conversion will be done from Decimal to Binary.

C Program to Convert Octal to Binary

 #include<stdio.h>
 #include<math.h>
 int main()
 {
     int oct, dec=0, bin=0, i=0;

     printf("Enter Octal Number: ");
     scanf("%d",&oct);

     // convert octal to decimal
     while(oct != 0)
     {
         dec += (oct%10) * (pow(8,i));
         oct /= 10;
         i++;
     }

     // now convert decimal to binary
     i=1;
     while(dec != 0)
     {
         bin += (dec%2) * i;
         dec /= 2;
         i *= 10; 
     }

     printf("Binary Value=%d",bin);

     return 0;
 }

Output:-

Enter Octal Number: 567
Binary Value= 101110111

Using Function

A function is a block of code that performs a specific task. For example, the main is a function and every program execution starts from the main function in C programming.

The C program is made of one or more functions. Every program must have at least one function with the name main. The execution of the program always starts from the main function and ends with the main function. The main function can call other functions to do some special task.

The function is a small program is used to do a particular task. In C a big program divided into several small subroutines/functions/procedures. Hence C is a function-oriented programming language.

 #include<stdio.h>
 #include<math.h>
 int convertOctalToDecimal(int m);
 int convertDecimalToBinary(int n);
 int main()
 {
     int octal, decimal, binary;

     printf("Enter Octal Number: ");
     scanf("%d",&octal);

     decimal = convertOctalToDecimal( octal );
     binary = convertDecimalToBinary( decimal );

     printf("Binary Value= %d", binary);

     return 0;
 }

 int convertOctalToDecimal(int m)
 {
     int dec=0, i=0;

     while(m != 0)
     {
         dec += (m%10) * pow(8,i);
         m /= 10;
         i++;
     }
     return dec;
 }

 int convertDecimalToBinary(int n)
 {
     int bin=0, j=1;

     while(n != 0)
     {
         bin += (n%2) * j;
         n /= 2;
         j *= 10;
     }
     return bin;
 }

C Program to convert Binary to Octal

First Binary will be converted to Decimal and after that conversion will be done from Decimal to Octal.

 #include<stdio.h>
 #include<math.h>
 int main()
 {
     int bin, dec=0, oct=0, i=0;

     printf("Enter Binary Number: ");
     scanf("%d", &bin);

     // first Convert Binary to Decimal
     while(bin != 0)
     {
         dec += (bin%10) * pow(2,i);
         bin /= 10;
         i++;
     }

     // now, Convert Decimal to Octal 
     i=1;
     while(dec != 0)
     {
         oct += (dec%8) * i;
         dec /= 8; 
         i *= 10;
     }

     printf("Octal Value = %d",oct);

     return 0;
 }

Output:-

Enter Binary Number: 110101001
Octal Value= 651

Binary to Octal using Function

 #include<stdio.h>
 #include<math.h>
 int convertBinaryToDecimal(int m);
 int convertDecimalToOctal(int n);
 int main()
 {
     int binary, decimal, octal;

     printf("Enter Binary Number: ");
     scanf("%d", &binary);

     decimal = convertBinaryToDecimal( binary );
     octal = convertDecimalToOctal( decimal );

     printf("Octal Value= %d\n",octal);

     return 0;
 }

 int convertBinaryToDecimal(int m)
 {
     int dec=0, i=0;

     while(m != 0)
     {
         dec += (m%10) * pow(2,i);
         m /= 10;
         i++;
     }
     return dec;
 }

 int convertDecimalToOctal(int n)
 {
     int oct=0, i=1;

     while(n != 0)
     {
         oct += (n%8) * i;
         n /= 8;
         i *= 10;
     }
     return oct;
 }

If you are using Linux/Unix OS and getting an error then read it “undefined reference to sqrt (or other mathematical functions)” even include math.h header

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest among 3 Numbers
Find Grade of student
Find the absolute value
Check Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Leave a Comment

Your email address will not be published. Required fields are marked *