**Write a program in c to convert a binary number(base 2) to octal number(base 8)**

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

/* * C program to convert binary numbers to octal numbers */ #include <stdio.h> #include <conio.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)); getch(); return 0; } /* Function to convert a binary number to octal number */ long binaryToOctal(long n) { /* First convert Binary to decimal number */ 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; } /*Now convert decimal to octal number */ i = 1; while(decimal != 0) { remainder = decimal%8; decimal = decimal/8; octal = octal + (remainder*i); i = i*10; } return octal; }

**Program Output**

Enter a binary number 110111 Octal number of 110111(binary) is 67

**Related Topics**