C Program to Convert Octal to Decimal

In this article, we will learn how to create a program in C that can convert any given octal number into its equivalent decimal number. At last, we will also take a look at a function-driven program that does the same job but uses a user-defined function named OctToDec().

But before going through the program. If you are not aware of

then refer to the step-by-step process for octal to decimal conversion. Now let's move on to the program.

Octal to decimal in C

To convert an octal number to a decimal number in C programming, you have to ask the user to enter the octal number first. Then convert it into a decimal number, and then display the equivalent value in decimal form as output.

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
    int octnum, decnum=0, i=0, rem;
    printf("Enter any Octal Number: ");
    scanf("%d", &octnum);
    while(octnum!=0)
    {
        rem = octnum%10;
        decnum = decnum + (rem*pow(8,i));
        i++;
        octnum = octnum/10;
    }
    printf("\nEquivalent Decimal Value = %d", decnum);
    getch();
    return 0;
}

Because the above program was written in the Code::Blocks IDE, you will receive the following output after a successful build and run. This is the first snapshot of the sample run:

c program convert octal to decimal

Now supply any octal number, say 345, as input and then press the ENTER key to see the output that shows you its equivalent decimal value as shown in the second snapshot given below:

c octal to decimal

Program Explained

Octal to Decimal in C without the pow() Function

You can also create the program without using the pow() function of the math.h library. Here is the program without using any pre-defined functions:

#include<stdio.h>
#include<conio.h>
int main()
{
    int octnum, decnum=0, rem, mul=1;
    printf("Enter any Octal Number: ");
    scanf("%d", &octnum);
    while(octnum!=0)
    {
        rem = octnum%10;
        decnum = decnum + (rem*mul);
        mul = mul*8;
        octnum = octnum/10;
    }
    printf("\nEquivalent Decimal Value = %d", decnum);
    getch();
    return 0;
}

Here is the final snapshot of the above program's sample run:

octal to decimal in c

Octal to Decimal in C using a User-Defined Function

Let's create a function-driven program in which the function OctToDec() is created in such a way that it takes one argument as an octal number, converts it into its equivalent decimal value, and then returns it to the main() function, as shown in the program given below:

#include<stdio.h>
#include<conio.h>
int OctToDec(int oct);
int main()
{
    int octnum, decnum=0, rem, mul=1;
    printf("Enter any Octal Number: ");
    scanf("%d", &octnum);
    decnum = OctToDec(octnum);
    printf("\nEquivalent Decimal Value = %d", decnum);
    getch();
    return 0;
}
int OctToDec(int oct)
{
    int rem, dec=0, mul=1;
    while(oct!=0)
    {
        rem = oct%10;
        dec = dec + (rem*mul);
        mul = mul*8;
        oct = oct/10;
    }
    return dec;
}

The final snapshot of the above program's sample run is given below:

c octal to decimal using function

The same program in different languages

C Quiz


« Previous Program Next Program »


Liked this post? Share it!