Number Systems conversions in C++
//Number Systems conversions in C++ (REST PART WILL BE UPDATED SOON)
#include <iostream>
#include<math.h>
using namespace std;
int convertB2D(long long int);
int convertD2O(int);
int convertD2H(int);
long long int convertD2B(int);
int convertO2D(int);
int main()
{
int o;
cout << "Hello Sir Welcome!" << endl;
cout<<endl<<"Press 1 to convert Binary to decimal"<<endl;
cout<<endl<<"Press 2 to convert Binary to octal"<<endl;
// cout<<endl<<"Press 3 to convert Binary to hexadecimal"<<endl;
cout<<endl<<endl;
cout<<endl<<"Press 4 to convert decimal to binary"<<endl;
cout<<endl<<"Press 5 to convert decimal to octal"<<endl;
// cout<<endl<<"Press 6 to convert decimal to hexadecimal"<<endl;
cout<<endl<<endl;
cout<<endl<<"Press 7 to convert octal to binary"<<endl;
cout<<endl<<"Press 8 to convert octal to decimal"<<endl;
// cout<<endl<<"Press 9 to convert octal to hexadecimal"<<endl;
cin>>o;
switch(o)
{
case 1:
long long int b;
cout<<endl<<"convert: binary to decimal"<<endl<<"Please enter binary number "<<endl;
cin>>b;
cout<<endl<<"decimal of "<<b<<"="<<convertB2D(b);
break;
case 2:
long long int b1;
int dec;
cout<<endl<<"convert: binary to octal"<<endl<<"Please enter binary number "<<endl;
cin>>b1;
dec=convertB2D(b1);
cout<<endl<<"octal of "<<b1<<"="<<convertD2O(dec);
break;
/* case 3:
long long int b2;
int dec1;
cout<<endl<<"convert: binary to hexadecimal"<<endl<<"Please enter binary number "<<endl;
cin>>b2;
dec1=convertB2D(b2);
cout<<endl<<"hexadecimal of "<<b2<<"="<<convertD2H(dec1);
break;*/
case 4:
int d;
cout<<endl<<"convert: decimal to binary"<<endl<<"Please enter decimal number "<<endl;
cin>>d;
cout<<endl<<"binary of "<<d<<"="<<convertD2B(d);
break;
case 5:
int d1;
cout<<endl<<"convert: decimal to octal"<<endl<<"Please enter decimal number "<<endl;
cin>>d1;
cout<<endl<<"octal of "<<d1<<"="<<convertD2O(d);
break;
/* case 6:
int d2;
cout<<endl<<"convert: decimal to hexadecimal"<<endl<<"Please enter decimal number "<<endl;
cin>>d2;
cout<<endl<<"hexadecimal of "<<d2<<"="<<convertD2H(d);
break;*/
case 7:
int oc,dec_temp;
cout<<endl<<"convert:octal to binary "<<endl<<"Please enter octal number "<<endl;
cin>>oc;
dec_temp=convertO2D(oc);
cout<<endl<<"binary of "<<oc<<"="<<convertD2B(dec_temp);
break;
/* case 8:
int oc2;
cout<<endl<<"convert:octal to decimal "<<endl<<"Please enter octal number "<<endl;
cin>>oc2;
cout<<endl<<"decimal of "<<oc2<<"="<<convertO2D(oc2);
break;*/
default:
cout<<endl<<"wrong input";
}
return 0;
}
int convertB2D(long long int b)
{
int i,rem,dec=0;
while(b!=0)
{
rem=b%10;
b=b/10;
dec=dec+rem*pow(2,i);
i++;
}
return dec;
}
int convertD2O(int d)//eg. 85
{
int rem,oct=0,base=1;
while(d!=0)
{
rem=d%8;
d=d/8;
oct=oct+rem*base;
base=base*10;
//oct=oct+rem*1
//oct+rem*10=25
//oct+rem*100=125
}
return oct;
}
int convertD2H(int d)//eg. 85
{
int rem,hex=0,base=1;
while(d!=0)
{
rem=d%16;
d=d/16;
hex=hex+rem*base;
base=base*10;
}
return hex;
}
long long int convertD2B(int d)
{
int rem,i=1;
long long int binary=0;
while(d!=0)
{
rem=d%2;
d=d/2;
binary=binary+rem*i;
i=i*10;
/*
decimal =12
0
0
1
1
(move upwards)
bin=1100
bin=bin+rem(0)*1=0
bin=bin+rem(0)*10=0
bin=bin+rem(1)*100=100
bin=bin+rem(1)*1000=1100
*/
}
return binary;
}
int convertO2D(int oct)
{
int i,rem,dec=0;
while(oct!=0)
{
rem=oct%10;
oct=oct/10;
dec=dec+rem*pow(8,i);
i++;
}
return dec;
}
👍
ReplyDelete