C Programming Code Examples C > Conversions Code Examples C Program to Convert Binary to Octal Number System C Program to Convert Binary to Octal Number System In this program, user is asked to enter the binary number and the program then converts that binary number to the octal number by calling a user defined function. To understand this program, you should be familiar with the following C programming concepts: #include <stdio.h> #include <math.h> //This function converts binary number to octal number int binaryToOctal(long binarynumber) { int octalnumber = 0, decimalnumber = 0, i = 0; /* This while loop converts binary number "binarynumber" to the * decimal number "decimalnumber" */ while(binarynumber != 0) { decimalnumber = decimalnumber + (binarynumber%10) * pow(2,i); i++; binarynumber = binarynumber / 10; } //i is re-initialized i = 1; /* This loop converts the decimal number "decimalnumber" to the octal * number "octalnumber" */ while (decimalnumber != 0) { octalnumber = octalnumber + (decimalnumber % 8) * i; decimalnumber = decimalnumber / 8; i = i * 10; } //Returning the octal number that we got from binary number return octalnumber; } int main() { long binarynumber; printf("Enter a binary number: "); scanf("%ld", &binarynumber); // calling the function here printf("Equivalent octal value: %d", binaryToOctal(binarynumber)); return 0; }