# Binary To Octal Conversion | C Program

## Binary to Octal conversion in C

There are two approaches of converting an octal number into a binary number like

• grouping the binary number into pairs of three and converting into octal.
• By converting the binary number into a decimal number first and then convert that decimal number into an octal number.

We are using second approach, this approach is long but it is very easy to implement and also the chances of error are also very minimum.

Example:10110

Decimal number     =1*(2^4) + 0*(2^3) + 1*(2^2) + 1*(2^1)+0*(2^0) = 16 + 0 + 4 + 2 + 0  22

Octal conversion    → 22/8, Remainder = 6, Quotient = ,→  2/8, Remainder = 2, Quotient = 0

i.e 10110 in binary is 26 in octal. ## Algorithm:-

1. User gives an input(binary number).
2. Input is stored in a long type variable say binary.
3. First we convert binary in decimal
1. A loop is started to convert binary_num into a decimal number.
1. Digits of binary are extracted one by one starting from right.

rem = binary_num %10;

2. Extracted digits are multiplied with j, i.e is correspond to power of2. After multiplying each digit with proper base, the results are added and stored in another variable say decimal_num.

decimal_num += rem*j;

4. Another loop is started to convert decimal_num into an octal number.

1. The number is divided by 8 and the remainder is stored.

rem = decimal_num % 8;

2. The octal number is constructed using the remainder.

octal_num += rem * j;

5. The loop will run until the number is not equal to 0.

while (decimal_num != 0)

6. octal is returned and printed as output. ## Code to convert binary to octal conversion in C

`/** C Program to Convert Binary to Octal*/#include<stdio.h>  int main(){   //For initialize variables   long int binary_num, decimal_num = 0, octal_num = 0, j = 1, rem;      //Inserting the binary number from the user   printf("Enter a binary number: ");   scanf("%ld", &binary_num);   // while loop for number conversion(binary to decimal)    while(binary_num != 0)    {        rem = binary_num % 10;        decimal_num = decimal_num + (rem * j);        //j*=2        j = j * 2;        //binary_num/10;        binary_num = binary_num / 10;            }        j=1;        //converting decimal to octal    while (decimal_num != 0)    {            rem = decimal_num % 8;            octal_num += rem * j;            decimal_num /= 8;            j *= 10;    }    printf("Equivalent octal value: %ld", octal_num);return 0;}`
`Output :Enter a binary number : 10101Equivalent octal value: 25`

### 4 comments on “Binary To Octal Conversion | C Program”

• Ravi kumar

#include
#include
int convert(long long bin);
int main() {
long long bin;
printf(“Enter a binary number: “);
scanf(“%lld”, &bin);
printf(“%lld in binary = %d in octal”, bin, convert(bin));
return 0;
}

int convert(long long bin) {
int oct = 0, dec = 0, i = 0;

// converting binary to decimal
while (bin != 0) {
dec += (bin % 10) * pow(2, i);
++i;
bin /= 10;
}
i = 1;

// converting to decimal to octal
while (dec != 0) {
oct += (dec % 8) * i;
dec /= 8;
i *= 10;
}
return oct;
} 2
• Kritika pandit

This code is wrong as it converts binary to decimal only
when input is 10101001 the above code prints 169 as its octal no.which is wrong. 4
• Bhoma

definately this code is binary to decimal conversion 3
• HelpPrepInsta

Sorry for the mistake guys, we’ll fix it up asap. Thank you 1