Tuesday, 19 November 2013

Source code: Conversion of decimal to Binary and Binary to decimal using C++ { conversion of 10 based integer to 2 based integer and vice versa } [ user friendly, easy, object oriented ]

#include<iostream>
using namespace std;

void d2b()
{
long decimal, d12, rem, x=1, sum=0;

cout <<"pick a decimal integer: "; cin>> decimal;
d12= decimal;

while(decimal>0)
{
rem= decimal % 2;
sum+= ( x*rem );
decimal/= 2;
x*= 10;
}
cout <<"\n\nBinary of " <<d12 <<" is: " <<sum <<endl <<endl;
}

void b2d()
{
long binary, m35, rem, y=1, sum=0;

cout <<"pick a binary number: "; cin>> binary;
m35= binary;

while(binary !=0)
{
rem= binary % 10;
sum+= (y*rem);
binary/=10;
y*=2;
}
cout <<"\n\ndecimal of " <<m35 <<" is: " <<sum <<endl <<endl;
}

int main()
{
char c;
cout <<"make your pick: \n";
cout <<"a. decimal to Binary.\n";
cout <<"b. Binary to decimal.\npick: ";
cin>> c;

switch(c)
{
case 'a': d2b(); break;
case 'b': b2d(); break;
}
}

No comments:

Post a Comment