Learn about C Program to convert Binary to Octal in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
C Program to convert Binary to Octal
Program:
#include<stdio.h>
#include<math.h>
int main()
{
int bin, dec=0, oct=0, i=0;
printf("Enter Binary Number: ");
scanf("%d", &bin);
while(bin != 0)
{
dec += (bin%10) * pow(2,i);
bin /= 10;
i++;
}
i=1;
while(dec != 0)
{
oct += (dec%8) * i;
dec /= 8;
i *= 10;
}
printf("Octal Value = %d",oct);
return 0;
}
Output:
Enter Binary Number: 1011
Octal Value = 13
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
C Program to Convert Octal to Binary using function
C Program to Convert Octal to Binary