# C++ Program to Convert Octal Number to Decimal Number

In the previous article, we have discussed about C++ Program to Convert Decimal Number to Binary Number. Let us learn how to Convert Octal Number to Decimal Number in C++ Program.

## Methods to Convert Octal Numbers to Decimal Numbers in C++

In this article, we will discuss different methods of converting octal to decimal numbers in c++. The list of the methods that we will discuss is given below.

Let discuss the basic idea for converting octal to a decimal number which will help us later in writing the code. Let discuss the approach with the help of an example. Suppose the octal number is 67 then its decimal form is 55. We will write 67 as 6*(8^1)+7*(8^0) which is equivalent to 55. So here we see we extract the digit of decimal number and multiply the digit with the power of 8 in increasing order. This means the first digit is multiplied with 8 to the power 0 while the second digit is multiplied with 8 to the power 1 and so on. So this is the intuition. Let’s discuss different methods to do it.

### Method 1-Using Loop with arithmetic operator

In this approach, we will take the help of a loop and modulo(%) and division(/) operator to extract the digit of the binary number. When we extract the digit we will simply multiply the digit with a power of 8 and stored the result in the variable. Let’s write the code for this.

#include <bits/stdc++.h>
using namespace std;

int convertOctalToDecimal(long long n)
{
int decimalNumber = 0, i = 0, rem;
while (n!=0)
{
rem = n%10;
n /= 10;
decimalNumber += rem*pow(8,i);
i++;
}
return decimalNumber;
}

int main()
{
long long n=67;
cout << n << " in octal is " << convertOctalToDecimal(n) << " in decimal";
return 0;
}

Output

67 in octal is 55 in decimal

### Method 2-Using pre-defined function

In c++ there is a pre-defined function that is used to convert octal to decimal numbers. Here we are talking about stoi() function. Let us see what the stoi() function is.stoi() stands for the string to integer, it is a standard library function in C++ STL, it is used to convert a given string in various formats (like binary, octal, hex, or a simple number in string formatted) into an integer. Let’s write the code for this.

#include <bits/stdc++.h>
using namespace std;

int main()
{
string n="67";
cout << n << " in octal is " <<  stoi(n, 0, 8) << " in decimal";

return 0;
}

Output

67 in octal is 55 in decimal

So these are the methods to convert octal numbers to decimal numbers in c++.