Example
Input
Input binary number: 00110111
Output
Octal number: 67
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 |
Step by step logic to convert binary to octal number system.
- Input binary number from user. Store it in a variable say binary.
- Initialize a variable to store converted octal say
octal = 0
. - Find 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 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 to 7 till
binary > 0
.
/**
* 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
*/