C++ PROGRAM
#include <iostream>
using namespace std;
void BinaryToDecimal(){
string binary;
cout<<"Enter Binary Number : "<<endl;
cin>>binary;
string num = binary;
int decimal = 0;
int base = 1;
int len = num.length();
for (int i = len - 1; i >= 0; i--) {
if (num[i] == '1')
decimal += base;
base = base * 2;
}
cout<<"Conversion of Binary Number "<<binary<<" to Decimal Number is: "<<decimal<<endl;
}
void DecimalToBinary(){
int decimal;
cout<<"Enter Decimal Number : "<<endl;
cin>>decimal;
int s=decimal;
int binary[32];
int i = 0;
while (decimal > 0) {
binary[i] = decimal % 2;
decimal = decimal / 2;
i++;
}
cout<<"Conversion of Decimal Number "<<s<<" to binary Number is: ";
for (int j = i - 1; j >= 0; j--)
cout << binary[j];
}
void HexadecimalToBinary(){
char hexa[10000];
cout<<"Enter Hexadecimal Number : "<<endl;
cin>>hexa;
long int i = 0;
cout<<"Conversion of Hexadecimal Number "<<hexa<<" to binary Number is: ";
while (hexa[i]) {
switch (hexa[i]) {
case '0':
cout << "0000";
break;
case '1':
cout << "0001";
break;
case '2':
cout << "0010";
break;
case '3':
cout << "0011";
break;
case '4':
cout << "0100";
break;
case '5':
cout << "0101";
break;
case '6':
cout << "0110";
break;
case '7':
cout << "0111";
break;
case '8':
cout << "1000";
break;
case '9':
cout << "1001";
break;
case 'A':
case 'a':
cout << "1010";
break;
case 'B':
case 'b':
cout << "1011";
break;
case 'C':
case 'c':
cout << "1100";
break;
case 'D':
case 'd':
cout << "1101";
break;
case 'E':
case 'e':
cout << "1110";
break;
case 'F':
case 'f':
cout << "1111";
break;
default:
cout << "\nInvalid hexadecimal digit "<<hexa[i];
}
i++;
}
}
void BinaryToHexadecimal(){
int Binary;
cout<<"Enter Binary Number : "<<endl;
cin>>Binary;
int s=Binary;
int hexa[10000];
int i = 1, j = 0, rem, dec = 0;
while (Binary > 0)
{
rem = Binary % 2;
dec = dec + rem * i;
i = i * 2;
Binary = Binary / 10;
}
i = 0;
while (dec != 0)
{
hexa[i] = dec % 16;
dec = dec / 16;
i++;
}
cout<<"Conversion of Binary Number "<<s<<" to Hexadecimal is: ";
for (j = i - 1; j >= 0; j--)
{
if (hexa[j] > 9)
{
cout<<(char)(hexa[j] + 55);
}
else
{
cout<<hexa[j];
}
}
}
int main() {
int i;
cout<<"Enter your choice: "<<endl;
cout<<"1. From Binary to Decimal\n"<<"2. From Decimal to Binary\n"<<"3. From Hexadecimal to Binary\n"<<"4. From Binary to Hexadecimal"<<endl;
cin>>i;
if(i==1){
BinaryToDecimal();
}
else if(i==2){
DecimalToBinary();
}
else if(i==3){
HexadecimalToBinary();
}
else if(i==4){
BinaryToHexadecimal();
}
return 0;
}
EXAMPLES :
INPUT 1 :
OUTPUT1:
INPUT2:
OUTPUT2:
INPUT3:
OUTPUT3:
INPUT4:
OUTPUT4:
Get Answers For Free
Most questions answered within 1 hours.