C Exercises: Convert an octal number into binary
54. Octal to Binary Conversion
Write a program in C to convert an octal number into binary.
This C program converts an octal number into its equivalent binary representation. It prompts the user to input an octal number, then iterates through each digit to calculate its decimal equivalent using a for loop. Afterward, it converts the decimal number obtained to binary format, storing and printing both the original octal number and its binary equivalent.
Visual Presentation:

Sample Solution:
C Code:
Output:
Convert Octal to Binary: ------------------------- Input an octal number (using digit 0 - 7) :57 The Octal Number : 57 The equivalent Binary Number : 101111
Explanation:
Here's a brief explanation of the above code:
- Header and Declaration:
- Includes the necessary header files ('stdio.h' for input/output and "math.h" for mathematical functions).
- Declared variables: 'n1' (input octal number), 'n5' (backup of the input), 'p' (power of 8 multiplier), 'dec' (resulting decimal number), i (loop variable for octal to decimal conversion), 'binno' (resulting binary number).
- User Input:
- Asks the user to input an octal number (using digits 0 - 7).
- Reads the input and stores it in the variable 'n1'.
- Octal to Decimal Conversion (using a for loop):
- Initializes i to 0 for the power of 8.
- Iterates through each digit of the octal number using a "for" loop.
- Calculates the contribution of each digit to the decimal number using the "pow()" function.
- Accumulates the result in the variable 'dec'.
- Decimal to Binary Conversion (using a while loop):
- Resets i to 1 for the power of 2.
- Repeatedly divides the decimal number ('dec') by 2 using a "while" loop.
- Calculates the remainder and adds it to the binary result ('binno').
- Updates to the power of 2.
- Continue until the decimal number becomes 0.
- Result Display:
- Prints the input octal number and its equivalent binary number.
- Return Statement:
- Returns 0 to indicate successful program completion.
Flowchart:

For more Practice: Solve these Related Problems:
- Write a C program to convert an octal number to binary by processing each digit into its 3-bit binary equivalent.
- Write a C program to convert an octal number to binary without using arrays by employing arithmetic operations.
- Write a C program to convert an octal number to binary and count the number of 1’s in the resulting binary number.
- Write a C program to convert an octal number to binary and display the output in groups of four digits.
C Programming Code Editor:
Previous: Write a program in C to convert a binary number to octal.
Next: Write a program in C to convert a decimal number to hexadecimal.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.