w3resource

C Exercises: Convert an octal number into binary

C For Loop: Exercise-54 with Solution

Write a program in C to convert an octal number into binary.

Visual Presentation:

Convert an octal number into binary

Sample Solution:

C Code:

#include 
#include 
int main() {
    // Declare variables
    long int n1, n5, p = 1;
    long int dec = 0, i;
    long int binno = 0;
    // Print output headers
    printf("\n\nConvert Octal to Binary:\n");
    printf("-------------------------\n");
    // Get octal input
    printf("Input an octal number (using digits 0 - 7): ");
    scanf("%ld", &n1);
    // Store input number
    n5 = n1;
    // Convert octal to decimal
    // Loop through each octal digit using a for loop
    for (i = 0; n1 > 0; n1 = n1 / 10, i++) {
        // Get current digit
        long int d = n1 % 10;
        // Update power of 8
        p = pow(8, i);
        // Add digit * power of 8 to result
        dec += d * p;
    }
    // Convert decimal to binary
    // Loop through each binary digit using a for loop
    for (i = 1; dec > 0; dec = dec / 2, i *= 10) {
        // Get remainder and add to result
        binno += (dec % 2) * i;
    }
    // Print octal and binary result
    printf("\nThe Octal Number: %ld\nThe equivalent Binary Number: %ld\n\n", n5, binno);
    return 0; // Indicate successful completion of the program
}

Sample 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:

Flowchart : Convert octal number into binary.

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.



Follow us on Facebook and Twitter for latest update.