# C++ Program to Convert Octal to Decimal

« Previous Program Next Program »

In this article, you will learn and get code on octal to decimal conversion in C++. The octal to decimal conversion program is created with and without using function.

But before starting the program, if you're not aware about the simple formula and steps used for the conversion, then you can refer to octal to decimal formula to get every required things.

## Octal to Decimal in C++

To convert octal number to decimal number in C++ programming, you have to ask from user to enter the octal number and then convert it into its equivalent decimal value. Print the equivalent decimal value on output as shown in the program given below.

The question is, write a program in C++ that converts octal number to 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 build and run under Code::Blocks IDE. Here is its sample run:

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

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

• Initial values, decimalNum=0, i=0
• And when user enters octal number as input, say 345, then it gets stored in octalNum variable. So octalNum=345
• Now the condition, octalNum!=0 or 345!=0 evaluates to be true, therefore program flow goes inside the while loop
• There, octalNum%10 or 345%10 or 5 gets initialized to rem. So rem=5
• And decimalNum + (rem*pow(8,i)) or 0+(5*8i) or 0+(5*80) or 5*1 or 5 gets initialized to decimalNum. So decimalNum=5
• The value of i gets incremented. So i=1
• And octalNum/10 or 345/10 or 34 gets initialized to octalNum. So octalNum=34
• Program flow goes back and evaluates the condition of while loop again
• The execution of while loop continues until its condition evaluates to be false
• Before its condition evaluates to be false, we'll get values (after each evaluation) as:
1. rem=5, decimalNum=5, i=1, octalNum=34
2. rem=4, decimalNum=37, i=2, octalNum=3
3. rem=3, decimalNum=229, i=3, octalNum=0
• Now print the value of decimalNum (229), that will be the decimal equivalent of given octal number (345)

### Without using pow() Function

To create the same program (as of previous one) without using 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 forgot to declare the variable mul at 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. Rest of the things will be same.

### Program to Deal with Invalid Octal Input

Octal number has its base as 8. Therefore it has total of 8 digits, that are from 0 to 7. Other digits like 8 and 9 is not a valid octal digit. Therefore if 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 its sample run with user input 349 as octal number. Because it contains 9 (the third digit of 349), and 9 is an invalid octal digit, therefore here is the output you will see:

### Octal to Decimal using Function

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

It returns 0, if there is any invalid octal digit present in the entered octal number, otherwise returns equivalent decimal value.

```#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 of previous program.

#### Same Program in Other Languages

C++ Online Test

« Previous Program Next Program »