Program to convert Octal to Decimal C++

How Program Works:

To convert an octal number to decimal number:

  • multiply each digits separately of octal number from right side by 8^0,8^1,8^2,8^3 … respectively and then find out the sum of them.

For Example we want to convert octal number 3401 to decimal number.

i) 1 * 8 ^0 = 1*1 =1

ii) 0 * 8 ^1 = 0*8 =0

iii) 4 * 8 ^2 = 4*64 =256

iv) 3 * 8 ^3 = 3*512 =1536

v) Sum= 1 + 0 + 256 + 1536 = 1793

  • So, (3401)8 = (1793)10

Program to convert Octal to Decimal C++:

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    long int octal,decimal =0;
    int i=0;
    cout<<"Enter any octal number: ";
    cin>>octal;
    cout<<endl;
    while(octal!=0)
    {
         decimal = decimal + (octal % 10) * pow(8,i++);
         octal = octal/10;
    }
    cout<<"Equivalent decimal value: "<<decimal<<endl;
   return 0;
}

 

OUTPUT:

Leave a Comment