Write a program in C++ to convert an octal number to a decimal without using an array

#include <iostream>
using namespace std;
void main()
{

 int n1, n5,p=1,k,ch=1;
int dec=0,i=1,j,d;
      cout<<"Convert Octal to Decimal:\n ";
cout<<"Input an octal number (using digit 0 - 7) :";
cin>>n1;
n5=n1;
    for(;n1>0;n1=n1/10)
    {
       k=n1 % 10;
       if(k>=8)
       {
        ch=0;
       }
     }
  switch(ch)
    {
    case 0 :
        cout<<"The number is not an octal number. \n";
        break;
    case 1:
        n1=n5;
for (j=n1;j>0;j=j/10)
{
          d = j % 10;
            if(i==1)
                  p=p*1;
            else
                 p=p*8;

  dec=dec+(d*p);
  i++;
}
        cout<<"The Octal Number : "<<n5<<"\nThe equivalent Decimal  Number : "<<dec<<endl;
        break;
    }
  cout<<"\n";
         system ("pause");
}


Test Data :
Input an octal number (using digit 0 - 7) :745
Expected Output :
The Octal Number : 745
The equivalent Decimal Number : 485

Write a program in C++ to convert an octal number to a decimal without using an array Write a program in C++ to convert an octal number to a decimal without using an array Reviewed by Unknown on June 20, 2017 Rating: 5

No comments:

Powered by Blogger.