C Programming Code Examples C > Conversions Code Examples Program to Convert Binary to Octal Program to Convert Binary to Octal In this program, we will first convert binary number to decimal. Then, the decimal number is converted to octal. #include <stdio.h> #include <math.h> int convertBinarytoOctal(long long binaryNumber); int main() { long long binaryNumber; printf("Enter a binary number: "); scanf("%lld", &binaryNumber); printf("%lld in binary = %d in octal", binaryNumber, convertBinarytoOctal(binaryNumber)); return 0; } int convertBinarytoOctal(long long binaryNumber) { int octalNumber = 0, decimalNumber = 0, j = 0; while(binaryNumber != 0) { decimalNumber += (binaryNumber%10) * pow(2,j); ++j; binaryNumber/=10; } j = 1; while (decimalNumber != 0) { octalNumber += (decimalNumber % 8) * j; decimalNumber /= 8; j *= 10; } return octalNumber; }