Binary to octal program in c is the Conversion program to convert binary to octal in c.

What is the binary number?
A number expressed in base 2 represented in 0 and 1 are binary numbers.
What is the octal number?
A number expressed in base 8. The numbers from 0 to 7 are octal numbers.

How to convert binary to octal
We convert binary to decimal as follow
1. Convert binary to decimal
Binary to decimal conversion: write down the powers of two start from right to left. Calculate power of 2. For each digit. Write down the final value of each power of two. Add the final values. This final result is our decimal number.

Binary to decimal formula
Decimal number = Add the power of 2 for each digit in the binary number.
2. convert decimal to octal :
Decimal to octal conversion in c: We divide the decimal number by 8 And write down the reminder. do this until the final result is 0.

Decimal to the octal formula :
Simply divide a binary number by 8 and write down a reminder.

Binary to octal algorithm

  1. Declare variable
  2. Convert Binary to decimal
  3. Convert Decimal to octal
  4. Print octal number

Binary to octal program in c

#include<stdio.h>

#include<math.h>

int ConvertFromBinarytoOther(int);

int ConvertFromDecimalToOther(int);

int main()

{

int final,num,binary,decimal;

printf("Enter Binary Number\n");

scanf("%d",&binary);

decimal=ConvertFromBinarytoOther(binary);//Converting binary to decimal

final=ConvertFromDecimalToOther(decimal);//Converting decimal to octal

//So binary to Octal

printf("Octal number of Binary number %d is %d\n",binary,final);

return(0);

}

int ConvertFromDecimalToOther(int decimal)

{

int temp,i,j=1,final=0;

temp=decimal;

while(temp!=0)

{

i=temp%8;

final=final+(i*j);

temp=temp/8;

j=j*10;

}

return(final);

}

int ConvertFromBinarytoOther(int binary)

{

int final=0,i,inc=0,temp;

temp=binary;

while(temp!=0)

{

i=temp%10;

final=final+(i*pow(2,inc));

temp=temp/10;

inc++;

}

return(final);

}

Output :

Binary to octal program in c

Write A Comment