Tuesday 4 December 2012

to convert binary number to decimal equivalent

#include<iostream>
using namespace std;
#include<conio.h>
#include<math.h>

 int main()
{ int n,i,sum,rem;
    sum=0,i=0;
    cout<<"enter the binary number   ";
    cin>>n;
  while(n!=0)
  { if((n%10==0) || (n%10==1))
      {rem=n%10;
      sum=sum+(rem*pow(2,i));
      n=n/10;
      ++i;
      }
      else {cout<<"entered number is not binary";goto a;}
  }
    cout<<"the decimal number is:"<<sum;
    a:
    getch();
}

No comments:

Post a Comment