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 Decimal to Octal

    C Programming Examples


    In this tutorial, you will learn how to write a C program that converts a decimal number (base 10) to its octal (base 8) equivalent. Understanding this conversion process is fundamental in computer science, especially in how data is processed and represented in different number systems.

    What is Decimal to Octal Conversion?

    Decimal to octal conversion involves changing a number from the decimal system, which uses digits from 0 to 9, to the octal system, which uses digits from 0 to 7. This conversion is often used in computer science and digital electronics.

    Algorithm to Convert Decimal to Octal

    1. Initialize Variables: Declare variables to store the decimal number, the octal equivalent, and a temporary variable for calculations.
    2. Input Decimal Number: Prompt the user to enter a decimal number. Use the scanf() function to take input.
    3. Conversion Process: Use a loop to divide the decimal number by 8. Store the remainder in the temporary variable. Append this remainder as the next digit of the octal number. Update the decimal number by dividing it by 8. Repeat until the decimal number is 0.
    4. Output Octal Number: Display the calculated octal number in reverse order using the printf() function.

    Code Example

    #include <stdio.h>
    
    int main() {
        int decimal, remainder;
        int octal[30]; // Array to store octal number
        int i = 0;
    
        // Input decimal number
        printf("Enter a decimal number: ");
        scanf("%d", &decimal);
    
        // Conversion process
        while (decimal != 0) {
            remainder = decimal % 8;
            octal[i] = remainder;
            decimal /= 8;
            i++;
        }
    
        // Display the octal number in reverse order
        printf("Octal equivalent: ");
        for (int j = i - 1; j >= 0; j--)
            printf("%d", octal[j]);
        printf("\n");
    
        return 0;
    }
    

    Program Output:

    For instance, if you input decimal 78, the program will convert and display the octal equivalent as 116.

    Enter a decimal number: 78
    Octal equivalent: 116
    

    Conclusion

    This tutorial demonstrates creating a C program for converting decimal numbers to octal. Understanding this conversion is crucial for those delving into computer architecture and digital systems, as it provides a foundational understanding of how data is represented and manipulated at a low level.



    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.