C program to convert number from Octal to Decimal

In this program, we will read Octal Values and converts it into Hexadecimal Number System. This program is for Octal to Decimal Conversion in C.

The logic behind to implement this program - Access each digit from the Number multiply the digit by the power of 8 (for first digits from right side multiply digit with 8^0, second digits 8^1 and so on), add the result and finally you will get Decimal value of given Octal Number. Here we will multiply with the power of base and base of Octal Number is 8.

For more details Learn: Computer Number System and its conversions.

Octal to Decimal Conversion using C program

/*C program to convert number from octal to decimal*/

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

int main()
{
    char oct[32] = { 0 };
    int dec, i;
    int cnt; /*for power index*/

    printf("Enter octal value: ");
    gets(oct);

    cnt = 0;
    dec = 0;
    for (i = (strlen(oct) - 1); i >= 0; i--) {
        dec = dec + (oct[i] - 0x30) * pow((double)8, (double)cnt);
        cnt++;
    }

    printf("DECIMAL value is: %d", dec);

    return 0;
}

Output:

    Enter octal value: 1041
    DECIMAL value is: 545

C Number System Conversion Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.