Binary To Octal Conversion | C Program

Binary to Octal conversion in C

There are two approaches of converting an Binary Number into a Octal Number like

• Converting Binary to Decimal and then Decimal to Octal
• By grouping Binary Number into Groups of 3 digits/bits and then converting to Octal

We will cover both approaches in this post

Method 1:-

We will first convert the Binary number into Decimal and then convert the Decimal number into Octal

Make sure that you have gone through these approaches below –

Method 1 Code

Run
```#include<stdio.h>
#include<math.h>

// function to convert binary to octal
void convert(long long num)
{
int octalDigit = 0, count = 1, i = 0, pos = 0;
int octalArray[32] = {0};

while(num != 0)
{
int digit = num % 10;

octalDigit += digit * pow(2, i);
i++;
num /= 10;

// placing current octalsum for 3 pair in array index position
octalArray[pos] = octalDigit;

// whenever we have read next 3 digits
// setting values to default
// increasing pos so next values can be placed at next array index
if(count % 3 == 0)
{
octalDigit = 0;
i = 0;
pos++;
}
count++;
}

// printing octal array in reverse order
for (int j = pos; j >= 0; j--)
printf("%d",octalArray[j]);

}

//main program
int main()
{
// long used rather than int to store large values
long long binary;

printf("Enter binary number: ");
scanf("%lld", &binary);

convert(binary);

return 0;
}```

Output :

`Enter binary number: 1010Decimal : 10Octal : 12`

Method 2:-

Method 2 uses the concept of grouping 3 successive digits/bits of the binary number and calculating octal digits against each grouping

We use an additional array to store the octal digits at each index.

We will need to print the array in reverse to get actual octal equivalent.

Method 2 Code

Run
```#include<stdio.h>
#include<math.h>
// function to convert binary to octal
void convert(long long num)
{
int octalDigit = 0, count = 1, i = 0, pos = 0;
int octalArray[32] = {0};

while(num != 0)
{
int digit = num % 10;

octalDigit += digit * pow(2, i);
i++;
num /= 10;

// placing current octalsum for 3 pair in array index position
octalArray[pos] = octalDigit;

// whenever we have read next 3 digits
// setting values to default
// increasing pos so next values can be placed at next array index
if(count % 3 == 0)
{
octalDigit = 0;
i = 0;
pos++;
}
count++;
}

// printing octal array in reverse order
for (int j = pos; j >= 0; j--)
printf("%d",octalArray[j]);

}

//main program
int main()
{
// long used rather than int to store large values
long long binary;

printf("Enter binary number: ");
scanf("%lld", &binary);

convert(binary);

return 0;
}```

Output :

`Enter binary number: 01010125`

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

8 comments on “Binary To Octal Conversion | C Program”

• Sahil

#include
#include
int main()
{
int binary,rem,decimal=0,octal=0 ,weight=1,num;
printf(“enter the binary number :\n”);
scanf(“%d”,&binary);

num=binary;

while(num!= 0)
{
rem=num%10;
decimal=decimal+rem*weight;
weight=weight*2;
num=num/10;
}
weight=1;
rem =0;
while(decimal!= 0)
{
rem = decimal%8;
octal = octal+rem*weight;
weight = weight * 10;
decimal = decimal/8;
}
printf(“the binary number =%d and its octal number is=%d”,binary,octal);
getch();
return 0;

}

• Somnath

#include
int main()
{
int binary,decimal=0,octal=0,m=1,n=1;;
scanf(“%d”,&binary);
while(binary>0)
{
decimal=decimal+(binary%10)*m;
m=m*2;
binary=binary/10;
if(m==8)
{
octal=octal+n*decimal;n=n*10;m=1;decimal=0;
}
}
octal=octal+n*decimal;
printf(“%d”,octal);
return 0;
}

• Narahari

This code is same as binary to decimal

binary to octal is as follows:
// Online C compiler to run C program online
#include
int binary2octal(int n);
int binary2decimal(int n);
int main()
{
int number,octal;
printf(“enter binary number”);
scanf(“%d”,&number);
octal= binary2octal(number);
printf(“bin to octal of %d is %d”,number,octal);

}
int binary2octal(int number)
{
int octal=0,value,i=1,lastdigit;
while(number !=0)
{
lastdigit=number%1000;
value= binary2decimal(lastdigit);
octal=octal+value*i;
i=i*10;
number=number/1000;
} return octal;
}
int binary2decimal(int number)
{
int dn=0,base=1,rem;
while(number!=0)
{
rem=number%10;
dn=dn+rem*base;
base=base*2;
number=number/10;
}
return dn;
}

• 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;
}

• 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.

• Bhoma

definately this code is binary to decimal conversion

• HelpPrepInsta

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

• Shailesh

#include
#include
int main()
{
int n,rem,d=0,b=0;
scanf(“%d”,&n);
while(n>0){
rem=n%10;
d=d+(pow(2,b)*rem);
b++;
n/=10;
}
int o=0,i=1;
while(d!=0){
o=o+((d%8)*i);
d=d/8;
i=i*10;

}
printf(“%d”,o);
return 0;
}

0