Write a C program to read binary number from user and convert to octal number system. How to convert from binary number system to octal number system in C. Logic to convert binary number to octal number system in C program.

**Example**

**Input**

Input binary number: 00110111

**Output**

Octal number: 67

## Required knowledge

Basic C programming, If else, For loop

### Binary Number System

Binary number system is a base 2 number system. Binary number system uses only 2 symbols to represent all its numbers i.e. 0 and 1.

### Octal Number System

Octal number system is a base 8 number system. Octal number system uses 8 symbols to represent all its numbers i.e. 0 1 2 3 4 5 6 7

## Logic to convert Binary to Octal number system

To make things simple and sound, I have divided the logic in two easy steps.

- Group all binary bits to 3 digits starting from right side.
- Write corresponding octal value for each grouped binary value.

**Binary to octal conversion table**

Binary | Octal |
---|---|

000 | 0 |

001 | 1 |

010 | 2 |

011 | 3 |

100 | 4 |

101 | 5 |

110 | 6 |

111 | 7 |

Below is the step by step logic to convert binary to octal number system.

- Input binary number from user. Store it in some variable say binary.
- Initialize a variable to store converted octal say octal = 0.
- Extract the last three digits of binary say digit = num % 1000.
- Find the octal equivalent (using binary to octal table) of the three binary digits found above.
- Add the octal value of binary found in above step to octal, by increasing the place value.
- Remove the last three digits of the binary number. Since they are processed, say binary = binary / 1000.
- Increase the place value of octal by using place = place * 10.
- Repeat step 3-7 till binary > 0.

## Program to convert Binary to Octal

```
/**
* C program to convert binary to octal number system
*/
#include <stdio.h>
int main()
{
int octalConstant[] = {0, 1, 10, 11, 100, 101, 110, 111};
long long binary, octal, tempBinary;
int digit, place, i;
octal = 0;
place= 1;
/*
* Input binary number from user
*/
printf("Enter any binary number: ");
scanf("%lld", &binary);
/* Copy original binary value to temp variable */
tempBinary = binary;
while(tempBinary != 0)
{
// Extract last three digit of binary
digit = tempBinary % 1000;
// Find octal equivalent of 3 digit binary
for(i=0; i<8; i++)
{
if(octalConstant[i] == digit)
{
/*
* Increase the place value of octal
* and add the previous octal value
*/
octal = (i * place) + octal;
break;
}
}
// Remove the last three digit of binary
tempBinary /= 1000;
// Increase the place value
place *= 10;
}
printf("Original binary number = %lld\n", binary);
printf("Octal number = %lld", octal);
return 0;
}
```

Output

Enter any binary number: 11001111 Original binary number = 11001111 Octal number = 317

Happy coding 😉

### Recommended post

- Loop programming exercises and solutions in C.
- C program to convert Binary to Decimal number system.
- C program to convert Binary to Hexadecimal number system.
- C program to convert Octal to Binary number system.
- C program to convert Octal to Decimal number system.
- C program to convert Octal to Hexadecimal number system.

<pre><code> ----Your Source Code---- </code></pre>