Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    C Program to Convert Octal to Binary

    C Programming Examples


    This tutorial will guide you through writing a C program that converts an octal number (base 8) to its binary (base 2) equivalent. This is a fundamental concept in computer science, especially in digital systems where different number systems are often used.

    What is Octal to Binary Conversion?

    Octal to binary conversion transforms a number expressed in the octal number system (base 8) into the binary number system (base 2). In the octal system, numbers are represented using digits ranging from 0 to 7, while the binary system uses only two digits, 0 and 1.

    Algorithm for Octal to Binary Conversion

    1. Initialize Variables: Declare variables to store the octal number, the binary equivalent, and a temporary variable.
    2. Input Octal Number: Prompt the user to enter an octal number. Use scanf() to take the input.
    3. Conversion Process:
      • For each digit of the octal number, convert it to a 3-digit binary number.
      • Append these binary sequences to form the complete binary number.
    4. Output Binary Number: Display the final binary number using printf().

    Code Example

    #include <stdio.h>
    
    int main() {
        int octal, binary = 0, decimal = 0, base = 1;
        int octalDigit, tempDecimal;
    
        // Input octal number
        printf("Enter an octal number: ");
        scanf("%d", &octal);
    
        // Octal to Decimal conversion
        while (octal != 0) {
            octalDigit = octal % 10;
            decimal += octalDigit * base;
            octal /= 10;
            base *= 8;
        }
    
        base = 1;
    
        // Decimal to Binary conversion
        while (decimal != 0) {
            tempDecimal = decimal % 2;
            binary += tempDecimal * base;
            decimal /= 2;
            base *= 10;
        }
    
        // Display the binary number
        printf("Binary equivalent: %d\n", binary);
    
        return 0;
    }
    

    Program Output:

    For example, if you input the octal number 123, the program will convert and display the binary equivalent.

    Enter an octal number: 123
    Binary equivalent: 1010011
    

    Conclusion

    This tutorial guided you on creating a C program that converts octal numbers to binary. Understanding different number systems like this is crucial for working in computing fields such as computer architecture and digital electronics.



    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.