Write a program in C++ to convert a binary number into a decimal number without using array, function and while loop. .

#include <iostream>
using namespace std;
void main()
{
       int n1, n,p=1;
int dec=0,i=1,j,d;

     cout<<"\n  Convert Binary to Decimal:\n ";
     cout<<"-------------------------\n";

cout<<"Input a binary number :";
cin>>n;
n1=n;
for (j=n;j>0;j=j/10)
{
          d = j % 10;
            if(i==1)
                  p=p*1;
            else
                 p=p*2;

  dec=dec+(d*p);
  i++;
}
        cout<<"\nThe Binary Number : "<<n1<<"\nThe equivalent Decimal  Number :  "<<dec<<endl;

      system ("pause");
}


Test Data : 
Input a binary number :1010101 
Expected Output :
The Binary Number : 1010101 
The equivalent Decimal Number : 85 

Write a program in C++ to convert a binary number into a decimal number without using array, function and while loop. . Write a program in C++ to convert a binary number into a decimal number without using array, function and while loop. . Reviewed by Unknown on June 11, 2017 Rating: 5

No comments:

Powered by Blogger.