Skip to content
Home » Convert Octal to Decimal using Function in C

Convert Octal to Decimal using Function in C

Learn about Convert Octal to Decimal using Function in C in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Convert Octal to Decimal using Function in C

In this code, we constructed a user defined function for converting octal to decimal. The program uses function to convert an octal number (given by the user) to a decimal number.

Source code:

#include <stdio.h>
#include <math.h>
long octalToDecimal(int octalnum)
{
    int decimalnum = 0, temp = 0;

    while(octalnum != 0)
    {
        decimalnum = decimalnum + (octalnum%10) * pow(8,temp);
        temp++;
        octalnum = octalnum / 10;
    }

    return decimalnum;
}
int main()
{
    int octalnum;

    printf("Enter octal number: ");
    scanf("%d", &octalnum);

    printf("Decimal value = %ld", octalToDecimal(octalnum));

    return 0;
}

Output:

Enter octal number: 36
Decimal value = 30

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
C program to convert octal to decimal
Decimal to Octal conversion in C using Function