Thursday 21 February 2013

Binary to Decimal and vice versa using function C++ program

| |


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

int main()
{
int b2d();
int d2b();
cout<<"\nBinary 2 Decimal"<<b2d()<<"\n";
cout<<"\nDecimal 2 binary " <<d2b();

system ("pause");}
int b2d()
{int a[10],i,no,sum=0,rem,j,k=0;

  cout<<"Enter Binary No:\n";
  cin>>no;
  i=0;
  while(no>0)
  {
  a[i]=no%10;
  no=no/10;
  i++;
  }
  for(j=0;j<i;j++)
  {
      sum=sum+a[j]*pow(2,k);
      k++;
  }
  cout<<"Decimal NO:"<<sum<<"\n";
      return no; }
   
   int d2b()
   {long dec,rem,i=1,sum=0;
    cout<<"Enter the decimal to be converted:";
    cin>>dec;
    do
    {
        rem=dec%2;
        sum=sum + (i*rem);
        dec=dec/2;
        i=i*10;
    }while(dec>0);
    cout<<"The binary of the given number is:"<<sum<<endl;
    cin.get();
    cin.get();
   return dec;}
   


Binary to Decimal and vice versa using function C++ program


Related Posts Plugin for WordPress, Blogger...
Powered by Blogger.