## Conversion of octal to binary in C

We will learn C program to convert octal to binary in this article. We will take an octal number as input from user then convert it to equivalent binary number using C program.

We know that octal number is 8 based number where we can use the digits from 0 to 7 for writing any number. On the other hand, binary is 2 based number where we have to use only 0 and 1 to write any number. Here, we will use this concept of their base to convert a number from octal to binary. So, let’s go ahead with code.

See details about how to convert octal to binary mathematically.

## C program to convert octal to binary

``````// C program to convert a number from octal to binary

#include <stdio.h>
#include <math.h>

int main(){
int oct, dec = 0, c = 0;
long bin = 0;

printf("Enter any octal number here to convert : ");
scanf("%d", &oct);

while(oct != 0){
dec = dec + (oct % 10) * pow(8, c);
c++;
oct /= 10;
}

c = 1;

while(dec != 0){
bin = bin + (dec % 2) * c;
dec /= 2;
c *= 10;
}

printf("\nBinary number is = %ld\n", bin);

return 0;
}``````

### Output of octal to binary conversion program: ## Octal to binary conversion using function in C

``````// Octal to binary using function in C

#include <stdio.h>
#include <math.h>

long oct_to_bin(int oct_num);

int main(){
int oct;

printf("Enter the octal number to convert : ");
scanf("%d", &oct);

printf("\nBinary number is = %ld\n", oct_to_bin(oct));

return 0;
}

long oct_to_bin(int octal_num){
int decimal_num = 0, k = 0;
long binary_num = 0;

while(octal_num != 0){
decimal_num = decimal_num + (octal_num % 10) * pow(8, k);
k++;
octal_num /= 10;
}

k = 1;

while (decimal_num != 0){
binary_num = binary_num + (decimal_num % 2) * k;
decimal_num /= 2;
k *= 10;
}

return binary_num;
}``````

### See the output of this program

``````Enter the octal number to convert : 56

Binary number is = 101110``````

Previous pageBinary to octal

Next pageAdd using pointers

Recommended for you: