Home » C Program to Convert Octal to Binary

C Program to Convert Octal to Binary

Learn about C Program to Convert Octal to Binary in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

C Program to Convert Octal to Binary

We must compute power to convert from Octal to Binary. As a result, we will employ the `pow()` function, which is defined in the standard library function `math.h`. First, Octal will be transformed to Decimal, followed by a conversion from Decimal to Binary.

Source code:

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

printf("Enter Octal Number: ");
scanf("%d",&oct);
while(oct != 0)
{
dec += (oct%10) * (pow(8,i));
oct /= 10;
i++;
}
i=1;
while(dec != 0)
{
bin += (dec%2) * i;
dec /= 2;
i *= 10;
}

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

return 0;
}``````

Output:

Enter Octal Number:20
Binary Value=10000

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.