#include <iostream>
#include <bits/stdc++.h>
using namespace std;
//function to convert from binary to decimal
void binaryTodec(int n){
int x=1,y,sum=0,temp=n;
while(temp>0){
y=temp%10;
sum+=x*y;
x*=2;
temp/=10;
}
cout<<"Binary number entered :"<<n<<" Decimal number is "<<sum<<endl;
}
//function to convert from octal to decimal
void octalToDecimal(int k){
int x=1,y,sum=0,temp=k;
while(temp>0){
y=temp%10;
sum+=x*y;
x*=8;
temp/=10;
}
cout<<"octal number entered :"<<k<<" Decimal number is "<<sum<<endl;
}
//function to convert from hexadecimal to decimal
void hexadecimalToDecimal(string s){
int sum=0,x=1,i;
int n=s.size();
for(i=n-1;i>=0;i--){
if(s[i]>='0'&&s[i]<='9')
{
sum+=x*(s[i]-'0');
}
else if(s[i]>='A'&&s[i]<='F')
{
sum+=x*(s[i]-'A'+10);
}
x*=16;
}
cout<<"hexadecimal number entered :"<<s<<" Decimal number is "<<sum<<endl;
}
int main()
{
int n,ch;
cout<<"Enter 1 .to convert from binary to decimal"<<endl<<"2. to convert from octal to decimal "<<endl<<"3 . to convert from hexadecimal to decimal"<<endl;
cin>>ch;
switch (ch){
case 1:{
int n;
cout<<"enter the binary number to convert";
cin>>n;
binaryTodec(n);
}
break;
case 2:{
int k;
cout<<"enter the octal number to convert";
cin>>k;
octalToDecimal(k);
}
break;
case 3:{
cout<<"enter the hexadecimal number to convert";
string s;
cin>>s;
hexadecimalToDecimal(s);
break;
}
default:
cout<<"enter a valid choice";
}
}
OUTPUT:
Enter 1 .to convert from binary to decimal
2. to convert from octal to decimal
3 . to convert from hexadecimal to decimal
1
enter the binary number to convert 10110
Binary number entered :10110 Decimal number is 22
OUTPUT:
Enter 1 .to convert from binary to decimal
2. to convert from octal to decimal
3 . to convert from hexadecimal to decimal
2
enter the octal number to convert 55
octal number entered :55 Decimal number is 45
OUTPUT:
Enter 1 .to convert from binary to decimal
2. to convert from octal to decimal
3 . to convert from hexadecimal to decimal
3
enter the hexadecimal number to convertAB2
hexadecimal number entered :AB2 Decimal number is 2738
OUTPUT:
Enter 1 .to convert from binary to decimal
2. to convert from octal to decimal
3 . to convert from hexadecimal to decimal
4
enter a valid choice
Comments