octal to decimal Algorithm

The octal to decimal algorithm is a simple and effective method used for converting a number expressed in the octal numeral system (base 8) to its equivalent in the decimal numeral system (base 10). The octal numeral system comprises of digits ranging from 0 to 7, and each position in an octal number represents a power of 8. In contrast, the decimal numeral system consists of digits ranging from 0 to 9, with each position representing a power of 10. The algorithm involves the manipulation of the positional values of the octal digits to obtain their decimal equivalent. To perform the conversion using the octal to decimal algorithm, the given octal number is processed from the rightmost digit to the leftmost. Each digit is multiplied by the appropriate power of 8, based on its position in the number (starting with 8^0 for the rightmost digit, 8^1 for the next digit to the left, and so on). The products obtained from these multiplications are then summed up to generate the decimal equivalent of the given octal number. This algorithm can be applied to any octal number, regardless of its length or complexity, making it a versatile and efficient tool for converting octal numbers to their decimal counterparts.
#include <math.h>
#include <stdio.h>

// Converts octal number to decimal
int convertValue(int num, int i) {
    return num * pow(8, i);
}

long long toDecimal(int octal_value) {

    int decimal_value = 0, i = 0;

    while (octal_value) {

        // Extracts right-most digit and then multiplies by 8^i
        decimal_value += convertValue(octal_value % 10, i++);

        // Shift right in base 10
        octal_value /= 10;
    }

    return decimal_value;
}

int main() {

    printf("Enter octal value: ");

    int octal_value;

    scanf("%d", &octal_value);

    long long result = toDecimal(octal_value);

    printf("%d in decimal is %lld\n", octal_value, result);

    return 0;
}

LANGUAGE:

DARK MODE: