✓ Link copied to clipboard
C 📅 July 9, 2026 ⏱ 6 min read

C Program to Convert Decimal to Octal

C Program to Convert Decimal to Octal

Converting a decimal number to its octal counterpart is a common task in programming when dealing with different number systems. So, let’s say you want to convert the decimal number 156 to octal. This program shows you how to do that using simple iteration and base conversion.

Prerequisites for the Decimal to Octal Conversion Program in C

Before starting with the code, make sure you meet the following requirements:

  • Knowledge of while loops for control flow
  • Understanding of the % and / operators for arithmetic expressions
  • Include the <stdio.h> library
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler

Source Code of C Program to Convert Decimal to Octal

Here's the complete C code to convert a decimal number to octal. It uses a while loop to repeatedly divide the number by 8 and collect the remainders, which form the octal number.

convert_decimal_to_octal.cC▶ Run
// Program to convert decimal to octal
#include <stdio.h>

int main() {
    int decimal, quotient, octal_number = 0, position = 1;

    // Input from user
    // Prompt the user to enter a decimal number
    printf("Enter a decimal number: ");
    scanf("%d", &decimal);

    // Copy the input number to quotient
    quotient = decimal;

    // Convert decimal to octal
    while (quotient != 0) {
        int remainder = quotient % 8;
        octal_number += remainder * position;
        quotient /= 8;
        position *= 10;
    }

    // Output the octal number
    printf("Octal number: %d\n", octal_number);

    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Decimal to Octal Conversion Program

When you run the program with the input '156', here's what you get:

Output
Enter a decimal number: 156
Octal number: 234
Output of C Program to Convert Decimal to Octal
Output of C Program to Convert Decimal to Octal

Algorithm to Convert Decimal to Octal in C

Converting a decimal number to octal involves dividing the number by 8 and storing the remainders. The remainders are collected in reverse order to form the octal number. Here is the exact logic this program uses, step by step.

  1. Read the decimal number from the user.
  2. Initialize octal_number to 0 and position to 1.
  3. Repeat while the quotient is not 0:
    • Find remainder by quotient % 8.
    • Update octal_number by adding remainder * position.
    • Divide quotient by 8.
    • Multiply position by 10.
  4. Print the octal_number.
flowchart TD
  A([Start]) --> B[/Enter decimal number/]
  B --> C[quotient = decimal]
  C --> D[octal_number = 0]
  D --> E[position = 1]
  E --> F{"quotient != 0?"}
  F -->|Yes| G["remainder = quotient %% 8"]
  G --> H["octal_number = octal_number + remainder * position"]
  H --> I["quotient = quotient / 8"]
  I --> J["position = position * 10"]
  J --> F
  F -->|No| K[/Print octal_number/]
  K --> L([End])
Flowchart: the algorithm step by step

How the C Program to Convert Decimal to Octal Works

Let's break down the code to see how each part contributes to the conversion from decimal to octal.

  • #include <stdio.h>: Includes the standard input-output library for using printf and scanf.
  • int main(): The main function where the program execution begins.
  • int decimal, quotient, octal_number = 0, position = 1;: Declares and initializes the needed variables. Here, octal_number will store the result, and position helps place each digit correctly.
  • scanf: Reads the decimal number from the user.
  • quotient = decimal;: Copies the user input to quotient for processing.
  • The while loop: Continues as long as there's a quotient to process.
  • remainder = quotient % 8;: Finds the remainder when quotient is divided by 8.
  • octal_number += remainder * position;: Adds the new digit to the growing octal number.
  • quotient /= 8;: Prepares for the next iteration by reducing the quotient.
  • position *= 10;: Moves the position left by one digit place.
  • printf: Outputs the final octal number.
  • return 0;: Ends the program.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

Why do we multiply the remainder by the position in this program?

🎉Correct! Multiplying by the position ensures the digit is in the correct place value.
🤔Not quite. Think about how placing works in positional number systems.

Time and Space Complexity of the Decimal to Octal Conversion Program

Analyzing how efficient our program is can help us understand its behavior with larger inputs.

Time Complexity: O(log n). Here, n is the decimal number being converted. This is because the program repeatedly divides quotient by 8, halving the number of digits to process each time. For example, converting 156 to octal takes only a few steps.

Space Complexity: O(1). The program uses a fixed number of variables: decimal, quotient, octal_number, and position. These are four integers regardless of the size of the input number.

⚠️

Common mistake:Forgetting to multiply by the position leads to incorrect digit placement in the final octal number.

Key takeaway:Multiplying by position makes sure each digit in the octal number is correctly placed by its value.

FAQs About C Program to Convert Decimal to Octal

Here are some common questions students have about converting decimal to octal in C.

It puts each remainder in the right spot to form the final octal number.
Yeah, by changing the base to 2 instead of 8, you can convert decimal to binary.
The output will also be zero since zero in any base is zero.
Starting at 1 makes sure the initial digit is in the correct unit's place.
There’s no direct library function for decimal to octal in C; you gotta implement the logic.
The program doesn’t handle negative numbers. It's meant for non-negative integers.

If you found this program helpful, check out these related programs:

Conclusion

Converting decimal to octal in C is easy once you get how remainders and positions work. Explore octal stuff more: try converting octal back to decimal. Practice more to really nail these concepts.

To boost your understanding, take our C language quiz or try this code in the online compiler.