# C++ Program for Octal to Decimal Conversion

## Octal to Decimal Conversion in C++

Here, in this page we will discuss octal to decimal conversion  in C++. The concept of converting an octal number to a decimal number is very simple , extract the digits of the octal number (start from right for convenience )and multiply the digits with the proper base(power of 8) lastly add each of the results and we will get a converted number as an output.

Example: 2012(Octal number)

Explanation : Decimal number     = 2*(8^3) + 0*(8^2) + 1*(8^1) + 2*(8^0)= 1024 + 0 + 8 + 2

= 1034   ## Algorithm:-

1. User gives an input(octal number)
2. The input is stored in an int type variable say octal.
3. A function convert()  is called with octal as parameter.
1. Digits of octal are extracted one by one starting from right.

int rem = octal%10;

2. Extracted digits are multiplied with proper base i.e. power of 8.

int res = rem*pow(8,i);

3. After multiplying each digit with proper base the results are added and stored in another variable say decimal.

decimal += res;

4. The output is printed. ## C++ Code:-

`    //C++ Program    //Octal to decimal conversion    #include <iostream>    using namespace std;    // Function to convert octal number to decimal    int convert(int octal)    {        int decimal = 0, i = 0;        //converting octal to decimal        while (octal != 0)        {            int rem = octal % 10;            octal /= 10;            int res=rem*pow(8,i);            decimal += res;            i++;        }        return decimal;    }    //main program    int main()    {        int octal;        cout << "Enter an octal number: ";        //user input        cin >> octal;        //calling function        int decimal=convert(octal);        //printing output        cout << octal << " in octal = " << decimal << " in decimal";             return 0;    }`
`OutputEnter an octal number: 20222022 in octal = 1042 in decimal` 