In this source code example, we will write a code to convert the Octal to a binary number in the C programming language.
Convert Octal to Binary in C
In this C program, we will take input from the User or console and print the result to the output:
#include <math.h>
#include <stdio.h>
long octalToBinary(int octalnum)
{
int decimalnum = 0, i = 0;
long binarynum = 0;
while (octalnum != 0)
{
decimalnum = decimalnum + (octalnum % 10) * pow(8, i);
i++;
octalnum = octalnum / 10;
}
i = 1;
while (decimalnum != 0)
{
binarynum = binarynum + (long)(decimalnum % 2) * i;
decimalnum = decimalnum / 2;
i = i * 10;
}
return binarynum;
}
int main()
{
int octalnum;
printf("Enter an octal number: ");
scanf("%d", &octalnum);
// Calling the function octaltoBinary
printf("Equivalent binary number is: %d", octalToBinary(octalnum));
return 0;
}
Output:
Enter an octal number: 12
Equivalent binary number is: 1010