C Program to convert binary to octal number
Program
#include<stdio.h>
#include<math.h>
int binary_octal(long long binary)
{
int octal = 0, decimal = 0, i = 0;
// convert binary to decimal
while (binary != 0) {
decimal += (binary % 10) * pow(2, i);
++i;
binary /= 10;
}
i = 1;
// convert decimal to octal
while (decimal != 0) {
octal += (decimal % 8) * i;
decimal /= 8;
i *= 10;
}
return octal;
}
int main() {
long long binary;
printf("Enter a binary number: ");
scanf("%lld", &binary);
printf("Octal equivalent is:\t%d\n", binary_octal(binary));
return 0;
}
Output
$ gcc convert-binary-to-octal.c -lm
$ ./a.out
Enter a binary number: 1111
Octal equivalent is: 17