C++ Program to Convert Octal to Decimal

In this article, you will learn and get code for octal to decimal conversion in C++. The octal to decimal conversion program is designed both with and without functions.

But before starting the program, if you're not aware of the simple formula and steps used for the conversion, you can refer to the octal to decimal formula to get all the required information.

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 and then convert it into its equivalent decimal value. Print the equivalent decimal value on the output as shown in the program given below.

The question is: write a program in C++ that converts an octal number to a decimal number. Here is its answer:

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int octalNum, decimalNum=0, i=0, rem;
    cout<<"Enter the Octal Number: ";
    cin>>octalNum;
    while(octalNum!=0)
    {
        rem = octalNum%10;
        decimalNum = decimalNum + (rem*pow(8,i));
        i++;
        octalNum = octalNum/10;
    }
    cout<<"\nEquivalent Decimal Value: "<<decimalNum;
    cout<<endl;
    return 0;
}

This program was built and runs under the Code::Blocks IDE. Here is its sample run:

C++ program convert octal to decimal

Now supply the input as any octal number, say 345, and press the ENTER key to convert and print its equivalent decimal value as shown here in the snapshot of sample output given below:

octal to decimal c++

The dry run of the above program with octal input 345 goes like this:

Octal to decimal conversion in C++ without using the pow() function

To create the same program as the previous one without using the pow() function, just replace the following statement:

decimalNum = decimalNum + (rem*pow(8,i));
i++;

with the statement given below:

decimalNum = decimalNum + (rem*mul);
mul = mul*8;

Note: Don't forget to declare the variable mul at the start of the program. and initialize its initial value as 1.

Remove the declaration and initialization of variable i from the program. Also remove the header file, math.h. Everything else will remain the same.

Program to deal with invalid octal input

The octal number has an 8 as its base. Therefore, it has a total of eight digits, which are from 0 to 7. Other digits, like 8 and 9, are not valid octal digits. Therefore, if the user enters a number that contains 8 or 9, Then here is the program to deal with:

#include<iostream>
using namespace std;
int main()
{
    int octalNum, rem, temp, chk=0;
    cout<<"Enter the Octal Number: ";
    cin>>octalNum;
    temp = octalNum;
    while(temp!=0)
    {
        rem = temp%10;
        if(rem>=8)
        {
            chk++;
            break;
        }
        temp = temp/10;
    }
    if(chk==0)
    {
        int decimalNum=0, mul=1;
        while(octalNum!=0)
        {
            rem = octalNum%10;
            decimalNum = decimalNum + (rem*mul);
            mul = mul*8;
            octalNum = octalNum/10;
        }
        cout<<"\nEquivalent Decimal Value: "<<decimalNum;
    }
    else
        cout<<"\nInvalid Octal Digit!";
    cout<<endl;
    return 0;
}

Here is a sample run with 349 as an octal number as user input. Because it contains 9 (the third digit of 349) and 9 is an invalid octal digit, here is the output you will see:

c++ octal to decimal

Using a Function to Convert Octal to Decimal in C++

This is the last program on octal to decimal conversion in C++. It is created using a user-defined function, octalToDecimal(). The function takes an octal number as its argument and returns either its equivalent decimal value or 0.

It returns 0 if there is an invalid octal digit in the octal number that was entered. Otherwise, it returns the decimal value that the octal number would be in decimal.

#include<iostream>
using namespace std;
int OctalToDecimal(int);
int main()
{
    int octalNum, decimalNum;
    cout<<"Enter the Octal Number: ";
    cin>>octalNum;
    decimalNum = OctalToDecimal(octalNum);
    if(decimalNum==0)
        cout<<"\nInvalid Octal Digit!";
    else
        cout<<"\nEquivalent Decimal Value: "<<decimalNum;
    cout<<endl;
    return 0;
}
int OctalToDecimal(int octalNum)
{
    int temp, rem, chk=0;
    temp = octalNum;
    while(temp!=0)
    {
        rem = temp%10;
        if(rem>=8)
        {
            chk++;
            break;
        }
        temp = temp/10;
    }
    if(chk==0)
    {
        int decimalNum=0, mul=1;
        while(octalNum!=0)
        {
            rem = octalNum%10;
            decimalNum = decimalNum + (rem*mul);
            mul = mul*8;
            octalNum = octalNum/10;
        }
        return decimalNum;
    }
    else
        return 0;
}

This program produces the same output as the previous program.

The same program in different languages

C++ Quiz


« Previous Program Next Program »


Liked this post? Share it!