C Program to Convert Octal Number to Binary Number System

  • Write a C program to convert octal number to binary number system.
  • How to convert Octal number to Binary number in C.

Required Knowledge

Binary number system is a base 2 number system using digits 0 and 1 whereas Octal number system is base 8 and using digits from 0 to 7. Given an octal number as input from user convert it to binary number.

For Example

1203 in Octal is equivalent to 1010000011 in Binary number system.

Algorithm to convert Octal to Binary number

  • Create a mapping between octal digits and binary sequence {(0 => 000), (1 => 001), (2 => 010), (3 => 011), (4 => 100), (5 => 101), (6 => 110), (7 => 111)}
  • Now, replace each octal digit with it’s corresponding binary sequence as mentioned above.

For Example:
Octal number : 1203
replace 1 by (001), 2 by (010), 0 by (000) and 3 by (011)
Decimal number = 001010000011 = 1010000011

C program to convert a octal number to binary number

C Program to Convert Octal Number to Binary Number System

#include <stdio.h>  
   
int main() {  
    int octalDigitToBinary[8] = {0, 1, 10, 11, 100, 101, 110, 111};  
    long long octalNumber, binaryNumber = 0, position;  
    int digit;  
       
    /* Take an Octal Number as input from user */ 
    printf("Enter an Octal Number\n");  
    scanf("%ld", &octalNumber); 
   
    position = 1;  
    /* Convert Octal Number to Binary Number */ 
    while(octalNumber != 0) {
        digit = octalNumber % 10;
        binaryNumber = (octalDigitToBinary[digit] * position) + binaryNumber;  
        octalNumber /= 10;  
        position *= 1000;  
    }
 
    printf("Binary Number = %ld", binaryNumber);
     
    return 0;
}

Output

Enter an Octal Number
1203
Binary Number = 1010000011
Enter an Octal Number
1111
Binary Number = 1001001001