# C Program to Convert Binary Number to Octal Number System

Write a C program to read a binary number and convert binary number to octal number system.
Wap in C to convert a base 2 number to a base 8 number.

#### Required Knowledge

This program converts 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 using digits from 0 to 7. Given a binary number as input from user convert it to octal number.

##### For Example
0000011 in Binary is equivalent to 3 in Octal number system.

Algorithm to convert Binary to Octal number
• Divide the input binary number in set of three binary digits, starting from least significant digit to most significant digit.
• For every group of three digits, write the equivalent octal digit.

For Example:
Let Binary number is 11001100
Divide binary number in group of three from right side: (11)(001)(100)
Replace each group with equivalent octal digit : 314

## C program to convert a decimal number to octal number

```#include <stdio.h>

int main() {
/*Define an array of octal digits to binary */
int octalDigitToBinary[8] = {0, 1, 10, 11, 100, 101, 110, 111};

long binaryNumber, octalNumber = 0, binaryCopy;
int threeDigits, multiple, counter;

multiple = 1;

/*
* Take a binary number as input from user
*/
printf("Enter a Binary Number\n");
scanf("%ld", &binaryNumber);

/*
* Convert binary to octal number equivalent
*/
while(binaryNumber != 0) {
threeDigits = binaryNumber % 1000;
/*Search through octalDigitToBinary array */
for(counter = 0; counter < 8; counter++) {
if(octalDigitToBinary[counter] == threeDigits) {
octalNumber = (counter * multiple) + octalNumber;
break;
}
}
/* Remove last three digits */
binaryNumber = binaryNumber/1000;
multiple *= 10;
}
printf("Octal Mumber : %ld", octalNumber);

return 0;
}
```
Output
```Enter a Binary Number
11001100
Octal Mumber : 314
```
```Enter a Binary Number
0000011
Octal Mumber : 3
```

Related Topics