C Program to Convert Octal Number to Decimal and Vice-Versa

Converting numbers between different bases is a fundamental task in programming. In this article, we will explore various C Program to Convert Octal Number to Decimal and Vice-Versa. We will cover multiple approaches with examples, explanations, and outputs.

Prerequisites

Before diving into the examples, you should be familiar with:

  • Basic C syntax and structure.
  • Understanding of octal and decimal number systems.
  • Functions in C.

Understanding the Problem

Octal to Decimal Conversion

Octal numbers are represented in base-8, while decimal numbers are in base-10. Converting octal to decimal involves multiplying each digit by 8 raised to its positional power and summing the results.

Decimal to Octal Conversion

Converting decimal to octal involves repeatedly dividing the number by 8 and recording the remainders.

Example 1: Octal to Decimal Conversion Using Iteration

1.1 Explanation

In this example, we will convert an octal number (represented as a string) to a decimal number using an iterative approach.

1.2 Program: Iterative Octal to Decimal Conversion

C
#include <stdio.h>
#include <string.h>
#include <math.h>

int octalToDecimal(char *octal);

int main() {
    char octal[65];
    printf("Enter an octal number: ");
    scanf("%s", octal);

    int decimal = octalToDecimal(octal);
    printf("Decimal equivalent of %s is: %d\n", octal, decimal);
    return 0;
}

int octalToDecimal(char *octal) {
    int decimal = 0;
    int length = strlen(octal);

    for (int i = 0; i < length; i++) {
        decimal += (octal[length - i - 1] - '0') * pow(8, i);
    }

    return decimal;
}

1.3 Output

C
Enter an octal number: 157
Decimal equivalent of 157 is: 111

Example 2: Decimal to Octal Conversion Using Iteration

2.1 Explanation

In this example, we will convert a decimal number to an octal number using an iterative approach. We repeatedly divide the number by 8 and record the remainders.

2.2 Program: Iterative Decimal to Octal Conversion

C
#include <stdio.h>

void decimalToOctal(int decimal);

int main() {
    int decimal;
    printf("Enter a decimal number: ");
    scanf("%d", &decimal);

    printf("Octal equivalent of %d is: ", decimal);
    decimalToOctal(decimal);
    printf("\n");
    return 0;
}

void decimalToOctal(int decimal) {
    int octal[32];
    int index = 0;

    while (decimal > 0) {
        octal[index] = decimal % 8;
        decimal = decimal / 8;
        index++;
    }

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

2.3 Output

C
Enter a decimal number: 111
Octal equivalent of 111 is: 157

Example 3: Recursive Octal to Decimal Conversion

3.1 Explanation

This example demonstrates converting an octal number to a decimal number using recursion. The recursive function processes each digit and accumulates the result.

3.2 Program: Recursive Octal to Decimal Conversion

C
#include <stdio.h>
#include <string.h>

int octalToDecimalRecursive(char *octal, int length);

int main() {
    char octal[65];
    printf("Enter an octal number: ");
    scanf("%s", octal);

    int decimal = octalToDecimalRecursive(octal, strlen(octal));
    printf("Decimal equivalent of %s is: %d\n", octal, decimal);
    return 0;
}

int octalToDecimalRecursive(char *octal, int length) {
    if (length == 0) {
        return 0;
    }

    int lastDigit = octal[length - 1] - '0';
    return (lastDigit * pow(8, strlen(octal) - length)) + octalToDecimalRecursive(octal, length - 1);
}

3.3 Output

C
Enter an octal number: 157
Decimal equivalent of 157 is: 111

Example 4: Recursive Decimal to Octal Conversion

4.1 Explanation

This example converts a decimal number to octal using recursion. The recursive function handles the division and prints the octal digits.

4.2 Program: Recursive Decimal to Octal Conversion

C
#include <stdio.h>

void decimalToOctalRecursive(int decimal);

int main() {
    int decimal;
    printf("Enter a decimal number: ");
    scanf("%d", &decimal);

    printf("Octal equivalent of %d is: ", decimal);
    decimalToOctalRecursive(decimal);
    printf("\n");
    return 0;
}

void decimalToOctalRecursive(int decimal) {
    if (decimal == 0) {
        return;
    }

    decimalToOctalRecursive(decimal / 8);
    printf("%d", decimal % 8);
}

4.3 Output

C
Enter a decimal number: 111
Octal equivalent of 111 is: 157

Example 5: Conversion Using Standard Library Functions

5.1 Explanation

C’s standard library does not provide direct functions for octal-decimal conversion, but you can use functions like strtol to simplify the process.

5.2 Program: Using strtol for Octal to Decimal Conversion

C
#include <stdio.h>
#include <stdlib.h>

int main() {
    char octal[65];
    printf("Enter an octal number: ");
    scanf("%s", octal);

    int decimal = strtol(octal, NULL, 8);
    printf("Decimal equivalent of %s is: %d\n", octal, decimal);
    return 0;
}

5.3 Output

C
Enter an octal number: 157
Decimal equivalent of 157 is: 111

Conclusion

This article covered various C Program to Convert Octal Number to Decimal and Vice-Versa. We explored iterative and recursive approaches for both conversions and demonstrated how to use the strtol function for simplified conversion. By understanding these examples, you can tackle similar conversion problems and optimize your solutions for better performance.