# C Program to Convert Binary Numbers to Octal Numbers

Here is a C program to convert a binary number to octal number. Binary number system is a base 2 number system using digits 0 and 1 whereas octal number system is base 8 and uses digits from 0 to 7. Given a binary number as input from user we have to print the octal equivalent of input number.
For Example:
1111101 in Binary is equivalent to 175 in Octal number system.

Conversion of Binary to Octal number includes two steps. First of all, we have to convert binary number to decimal number then finally decimal number to octal number.
C program to convert binary to decimal numbers
C program to convert decimal to octal numbers

## C program to convert binary number to octal number

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

long binaryToOctal(long n);
int main() {
long binary;
printf("Enter a binary number\n");
scanf("%ld", &binary);
printf("Octal number of %ld(binary) is %ld",
binary, binaryToOctal(binary));

return 0;
}

long binaryToOctal(long n) {
int remainder;
long decimal = 0, octal = 0, i = 0;

while(n != 0) {
remainder = n%10;
n = n/10;
decimal = decimal + (remainder*pow(2,i));
++i;
}

i = 1;
while(decimal != 0) {
remainder = decimal%8;
decimal = decimal/8;
octal = octal + (remainder*i);
i = i*10;
}

return octal;
}
```
Output
```Enter a binary number
110111
Octal number of 110111(binary) is 67
```

Related Topics