Home » C Program to Convert Octal to Binary using function

# C Program to Convert Octal to Binary using function

Learn about C Program to Convert Octal to Binary using function 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 using function

Program:

`````` #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;
}``````

Output:

Enter Octal Number:16
Binary Value= 1110

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