 # C Programming Code Examples

## C > Conversions Code Examples

### C Program to Convert Octal Number to Binary Number

``` C Program to Convert Octal Number to Binary Number In this program we have created a user defined function octalToBinary(). This function converts the octal number (entered by user) to decimal number first and then converts that decimal number to binary number. To understand the working of this program, you should have the basic knowledge of following C Programming topics: #include <stdio.h> #include <math.h> //This function converts octal number to binary number long octalToBinary(int octalnumber) { int decimalnumber = 0, i = 0; long binarynumber = 0; /* This loop converts octal number "octalnumber" to the * decimal number "decimalnumber" */ while(octalnumber != 0) { decimalnumber = decimalnumber + (octalnumber%10) * pow(8,i); i++; octalnumber = octalnumber / 10; } //i is re-initialized i = 1; /* This loop converts the decimal number "decimalnumber" to the binary * number "binarynumber" */ while (decimalnumber != 0) { binarynumber = binarynumber + (decimalnumber % 2) * i; decimalnumber = decimalnumber / 2; i = i * 10; } //Returning the binary number that we got from octal number return binarynumber; } int main() { int octalnumber; printf("Enter an octal number: "); scanf("%d", &octalnumber); //Calling the function octaltoBinary printf("Equivalent binary number is: %ld", octalToBinary(octalnumber)); return 0; } ``` 