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

C Program to Convert Octal to Decimal: Full Code & Output

C Program to Convert Octal to Decimal: Full Code & Output

Converting numbers between systems is a skill you’ll use in both computer science and electronics. Here, we’re gonna convert an octal number, like 17, into its decimal form. Octal numbers are base-8, using digits 0-7, and decimals are base-10.

Prerequisites for the C Program to Convert Octal to Decimal

Before you start writing the program, ensure you know these:

  • Understanding of loops such as while in C.
  • Usage of basic mathematical operators like %, /, and *.
  • Familiarity with <stdio.h> for input/output.
  • 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 Octal to Decimal

Here's the source code for converting an octal number to its decimal equivalent. It calculates the decimal value by processing each digit.

octal_to_decimal.cC▶ Run
#include <stdio.h>

int main() {
    int octalNumber, decimalNumber = 0, base = 1, rem;

    // User input
    printf("Enter an octal number: ");
    scanf("%d", &octalNumber);

    // Convert octal to decimal
    while (octalNumber != 0) {
        rem = octalNumber % 10;
        decimalNumber += rem * base;
        base *= 8;
        octalNumber /= 10;
    }

    // Output the result
    printf("The decimal equivalent is: %d\n", decimalNumber);

    return 0;
}

Try this code yourself — free

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

▶ Open Compiler

Output of the Convert Octal to Decimal Program

When you run the program with an input, here's what you'll see:

Output
Enter an octal number: 17
The decimal equivalent is: 15
Output of C Program to Convert Octal to Decimal
Output of C Program to Convert Octal to Decimal

Algorithm to Convert Octal to Decimal in C

Converting octal to decimal means evaluating each digit by its position. Here's the step-by-step algorithm this C program follows:

  1. Read the octal number from the user.
  2. Initialize decimalNumber to 0 and base to 1.
  3. Repeat while octalNumber is not 0:
  4. Get the last digit using octalNumber % 10.
  5. Update decimalNumber by adding the product of the digit and base.
  6. Multiply base by 8.
  7. Remove the last digit from octalNumber by dividing by 10.
flowchart TD
  A([Start]) --> B[/Read octalNumber/]
  B --> C[decimalNumber = 0]
  C --> D[base = 1]
  D --> E{"octalNumber != 0?"}
  E -->|Yes| F[rem = octalNumber %% 10]
  F --> G[decimalNumber += rem * base]
  G --> H[base *= 8]
  H --> I[octalNumber /= 10]
  I --> E
  E -->|No| J([End])
Flowchart: the algorithm step by step

How the C Program to Convert Octal to Decimal Works

Let's break down how each part of the code converts octal to decimal:

  • #include <stdio.h>: Includes the standard input and output library for functions like printf and scanf.
  • int main(): The main function where program execution begins.
  • int octalNumber, decimalNumber = 0, base = 1, rem;: Declares variables for storing the octal number, the resulting decimal number, the base (initially set to 1), and remainder.
  • scanf("%d", &octalNumber);: Reads an octal number input from the user and stores it in octalNumber.
  • while (octalNumber != 0): Loops until all digits have been processed — a fundamental control structure for this conversion.
  • rem = octalNumber % 10;: Gets the last digit of octalNumber.
  • decimalNumber += rem * base;: Converts and accumulates the current digit's decimal value.
  • base *= 8;: Updates the base for the next digit's place value in octal.
  • octalNumber /= 10;: Removes the processed digit from octalNumber.
  • printf("The decimal equivalent is: %d\n", decimalNumber);: Outputs the calculated decimal number.
  • return 0;: Ends the program, returning zero as the status code.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What base is used in an octal number system?

🎉Correct! Octal numbers are in Base 8 which uses digits 0-7.
🤔Not quite. Think about how many unique digits octal uses.

Time and Space Complexity of the C Program to Convert Octal to Decimal

Evaluating the efficiency of converting octal to decimal helps understand its performance.

Time Complexity: O(n), where n is the number of digits in the octal number — for instance, 2 for 17. The while (octalNumber != 0) loop processes each digit once, so 3 digits means 3 rounds.

Space Complexity: O(1). The program uses a fixed number of variables — octalNumber, decimalNumber, base, and rem. There's no extra space needed beyond these few.

⚠️

Common mistake:Don’t forget to multiply base by 8 each time. It's important for weighing each digit accurately.

Key takeaway:Octal to decimal conversion’s all about assigning positional values to each digit and updating base as you go.

FAQs About C Program to Convert Octal to Decimal

Here's a collection of frequently asked questions about converting octal numbers to decimal in C.

Yeah, within the integer data type's range. For huge numbers, you’ll have to use other data types or techniques.
The program won’t give correct results since it thinks all input digits are valid octal numbers (0-7).
Base starts at 1 because the rightmost digit is at position 0, which means base0. Each leftward digit ups the power of 8.
C doesn’t give a direct library function for octal to decimal conversion, so it's common to write your own function.
You can check manually by converting the octal number to decimal on paper or with a calculator, then matching it with what the program outputs.

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

Conclusion

This program shows a cool way of using basic arithmetic to convert number systems. If this caught your interest, give converting binary to decimal a shot for more practice. Get the logic down, and you'll be set for more complex conversions.

Keep sharpening your C skills with quizzes on our platform. You can also check out the code in the EduSeekho online compiler anytime.