0
0
CProgramBeginner · 2 min read

C Program to Convert Decimal to Octal Number

To convert a decimal number to octal in C, repeatedly divide the number by 8 and store the remainders; then print these remainders in reverse order using code like while (decimal > 0) { octal[i++] = decimal % 8; decimal /= 8; }.
📋

Examples

Input10
Output12
Input64
Output100
Input0
Output0
🧠

How to Think About It

To convert decimal to octal, think of dividing the decimal number by 8 repeatedly. Each division gives a remainder between 0 and 7, which forms the octal digits from right to left. Collect these remainders and then reverse them to get the octal number.
📐

Algorithm

1
Get the decimal number input from the user.
2
If the number is 0, print 0 and stop.
3
While the decimal number is greater than 0, divide it by 8 and store the remainder.
4
Keep track of all remainders in order.
5
Print the stored remainders in reverse order to get the octal number.
💻

Code

c
#include <stdio.h>
int main() {
    int decimal, i = 0, octal[20];
    printf("Enter a decimal number: ");
    scanf("%d", &decimal);
    if (decimal == 0) {
        printf("Octal: 0\n");
        return 0;
    }
    int num = decimal;
    while (num > 0) {
        octal[i++] = num % 8;
        num /= 8;
    }
    printf("Octal: ");
    for (int j = i - 1; j >= 0; j--) {
        printf("%d", octal[j]);
    }
    printf("\n");
    return 0;
}
🔍

Dry Run

Let's trace the decimal number 10 through the code to convert it to octal.

1

Input

decimal = 10

2

Initialize

i = 0, num = 10

3

First division

num % 8 = 10 % 8 = 2; octal[0] = 2; num = 10 / 8 = 1; i = 1

4

Second division

num % 8 = 1 % 8 = 1; octal[1] = 1; num = 1 / 8 = 0; i = 2

5

Print octal

Print octal[1] then octal[0]: 1 2

IterationnumRemainder (num % 8)octal arrayi
1102[2]1
211[2, 1]2
💡

Why This Works

Step 1: Divide by 8 to find octal digits

Each division by 8 gives a remainder that is one octal digit, because octal is base 8.

Step 2: Store remainders in order

We store remainders in an array as we get them, which represent digits from least significant to most.

Step 3: Print digits in reverse

Since the first remainder is the last digit, we print the array backwards to get the correct octal number.

🔄

Alternative Approaches

Using recursion
c
#include <stdio.h>
void decToOctal(int n) {
    if (n == 0) return;
    decToOctal(n / 8);
    printf("%d", n % 8);
}
int main() {
    int decimal;
    printf("Enter a decimal number: ");
    scanf("%d", &decimal);
    if (decimal == 0) printf("0\n");
    else decToOctal(decimal);
    printf("\n");
    return 0;
}
Recursion prints digits in correct order without extra storage but may be harder to understand for beginners.
Using built-in printf formatting
c
#include <stdio.h>
int main() {
    int decimal;
    printf("Enter a decimal number: ");
    scanf("%d", &decimal);
    printf("Octal: %o\n", decimal);
    return 0;
}
Using %o in printf directly converts decimal to octal, simplest and fastest but less educational.

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

Time Complexity

The loop divides the number by 8 each time, so it runs about log base 8 of n times, which is O(log n).

Space Complexity

We store each remainder in an array, so space grows with the number of octal digits, also O(log n).

Which Approach is Fastest?

Using printf with %o is fastest and simplest, recursion uses call stack, and manual array method is educational but uses extra space.

ApproachTimeSpaceBest For
Manual division and arrayO(log n)O(log n)Learning and understanding conversion
RecursionO(log n)O(log n) (call stack)Elegant code, less storage management
printf %o format specifierO(1)O(1)Quick and practical conversion
💡
Remember to print the octal digits in reverse order after collecting remainders.
⚠️
Beginners often print remainders in the order collected, which reverses the octal number.