0
0
CProgramBeginner · 2 min read

C Program to Convert Octal to Decimal Number

To convert octal to decimal in C, read the octal number as an integer, then repeatedly extract digits using % 10, multiply by powers of 8, and sum them; for example, use a loop with decimal += digit * base and base *= 8.
📋

Examples

Input17
Output15
Input10
Output8
Input0
Output0
🧠

How to Think About It

To convert an octal number to decimal, treat each digit of the octal number as a value from 0 to 7. Starting from the rightmost digit, multiply it by 8 raised to the power of its position index (starting at 0). Sum all these values to get the decimal equivalent.
📐

Algorithm

1
Get the octal number as input.
2
Initialize decimal result to 0 and base to 1.
3
Extract the last digit of the octal number using modulus 10.
4
Multiply the digit by the current base and add to decimal result.
5
Multiply base by 8 for the next digit position.
6
Remove the last digit from the octal number by dividing by 10.
7
Repeat steps 3-6 until the octal number is 0.
8
Return the decimal result.
💻

Code

c
#include <stdio.h>

int main() {
    int octal, decimal = 0, base = 1, digit;
    printf("Enter an octal number: ");
    scanf("%d", &octal);

    while (octal > 0) {
        digit = octal % 10;
        decimal += digit * base;
        base *= 8;
        octal /= 10;
    }

    printf("Decimal number: %d\n", decimal);
    return 0;
}
Output
Enter an octal number: 17 Decimal number: 15
🔍

Dry Run

Let's trace the octal number 17 through the code

1

Initial values

octal = 17, decimal = 0, base = 1

2

First iteration

digit = 17 % 10 = 7; decimal = 0 + 7*1 = 7; base = 1*8 = 8; octal = 17 / 10 = 1

3

Second iteration

digit = 1 % 10 = 1; decimal = 7 + 1*8 = 15; base = 8*8 = 64; octal = 1 / 10 = 0

4

End loop

octal is 0, loop ends; decimal = 15

octaldigitdecimalbase
17778
111564
💡

Why This Works

Step 1: Extract digits

We use octal % 10 to get the last digit of the octal number because octal digits are base 10 digits representing base 8 values.

Step 2: Calculate decimal value

Each digit is multiplied by base, which starts at 1 and increases by powers of 8, reflecting the place value in octal.

Step 3: Update for next digit

We divide the octal number by 10 to remove the last digit and multiply base by 8 to move to the next higher place value.

🔄

Alternative Approaches

Using string input and processing each character
c
#include <stdio.h>
#include <string.h>

int main() {
    char octal[20];
    int decimal = 0, base = 1, length, i;
    printf("Enter an octal number: ");
    scanf("%s", octal);
    length = strlen(octal);

    for (i = length - 1; i >= 0; i--) {
        int digit = octal[i] - '0';
        decimal += digit * base;
        base *= 8;
    }

    printf("Decimal number: %d\n", decimal);
    return 0;
}
This method handles octal numbers as strings, avoiding integer input limits and allowing leading zeros.
Using built-in function strtol
c
#include <stdio.h>
#include <stdlib.h>

int main() {
    char octal[20];
    printf("Enter an octal number: ");
    scanf("%s", octal);
    int decimal = (int)strtol(octal, NULL, 8);
    printf("Decimal number: %d\n", decimal);
    return 0;
}
This uses the standard library function to convert base 8 string directly to decimal, simplifying code.

Complexity: O(n) time, O(1) space

Time Complexity

The program processes each digit of the octal number once, so the time grows linearly with the number of digits.

Space Complexity

Only a few integer variables are used, so space usage is constant regardless of input size.

Which Approach is Fastest?

Using strtol is fastest and simplest for string input, while manual digit extraction is good for integer input but limited by integer size.

ApproachTimeSpaceBest For
Manual digit extraction (integer input)O(n)O(1)Simple integer inputs without leading zeros
String processing with loopO(n)O(1)Handling large octal numbers and leading zeros
Using strtol functionO(n)O(1)Fastest and simplest for string inputs
💡
Always validate that octal digits are between 0 and 7 to avoid incorrect conversions.
⚠️
Beginners often forget to multiply each digit by the correct power of 8, leading to wrong decimal results.