Program in C to Convert Decimal to Octal

Program in C to Convert Decimal to Octal

Decimal is the base-10 number system we use in everyday life, while octal is a base-8 number system that uses digits from 0 to 7. Octal numbers are often used in programming and computer systems, particularly in older systems and in Unix file permissions. In this article, we will write a program in C to convert a decimal number into its octal equivalent.

Understanding The Problem

To convert a decimal number into octal, we repeatedly divide the number by 8 and store the remainders. Reading these remainders in reverse order gives us the octal number. For example, 25 in decimal is divided as 25 ÷ 8 = 3 remainder 1 and 3 ÷ 8 = 0 remainder 3, so the octal result is 31.

Program 1: Using Loops and Arrays

This method uses division by 8 repeatedly and stores remainders in an array. At the end, the array is printed in reverse to get the octal representation.

#include <stdio.h>

int main() {

    int n, octal[32], i = 0;

    printf("Enter a decimal number: ");
    scanf("%d", &n);

    while (n > 0) {

        octal[i] = n % 8;
        n = n / 8;
        i++;

    }

    printf("Octal equivalent: ");

    for (int j = i - 1; j >= 0; j--) {
        printf("%d", octal[j]);
    }

    printf("\n");

    return 0;

}

This program works by repeatedly dividing the number by 8, collecting remainders, and then printing them in reverse order, forming the octal number.

Program 2: Using Recursion

Recursion can also be used to convert decimal to octal. It divides the number until it reaches zero, then prints the remainders on the way back.

#include <stdio.h>

void decimalToOctal(int n) {

    if (n == 0)
        return;

    decimalToOctal(n / 8);

    printf("%d", n % 8);

}

int main() {

    int n;

    printf("Enter a decimal number: ");
    scanf("%d", &n);

    if (n == 0) {
        printf("0");
    } else {
        decimalToOctal(n);
    }

    printf("\n");

    return 0;

}

This recursive approach avoids arrays and prints the digits in the correct order directly.

Program 3: Using sprintf()

C’s standard library allows converting decimal to octal using format specifiers in sprintf(). This method stores the octal result as a string.

#include <stdio.h>

int main() {

    int n;
    char octal[100];

    printf("Enter a decimal number: ");
    scanf("%d", &n);

    sprintf(octal, "%o", n);  // Convert decimal to octal string

    printf("Octal equivalent: %s\n", octal);

    return 0;

}

Here, %o in sprintf() formats the decimal number into octal. This method is concise and useful when the octal value needs to be stored as text instead of printed immediately.

FAQs

Q1: Why do we use octal numbers in programming?
Octal was widely used in older computing systems because it represents binary in a more compact form, grouping three bits into one octal digit.

Q2: Which method is faster, loops or recursion?
The loop method is slightly faster, while the recursion method is shorter and easier to read.

Q3: Can this program handle negative numbers?
Not directly. Additional logic would be needed to manage negative values properly.

Q4: Why use sprintf() instead of printing directly?
sprintf() allows storing the octal number as a string, which is useful for further processing, saving to files, or network transmission.

Conclusion

Converting decimal to octal is a simple process of repeatedly dividing by eight and recording remainders. The iterative and recursive approaches are straightforward, while the sprintf() method provides a clean, string-based solution. Understanding all three techniques gives flexibility in handling decimal-to-octal conversions in C.

References & Additional Resources

Scroll to Top