In: Computer Science
code in c++
using the code given add a hexadecimal to binary converter and add a binary to hexadecimal converter
#include <iostream>
#include <string>
#include<cmath>
#include<string>
using namespace std;
int main()
{
string again;
do {
int userChoice;
cout << "Press 2 for Decimal to Binary"<< endl;
cout << "Press 1 for Binary to Decimal: ";
cin >> userChoice;
if (userChoice == 1)
{
long n;
cout << "enter binary number" << endl;
cin>>n;
int decnum=0, i=0, remainder;
while(n!=0)
{
remainder=n%10;
n=n/10;
decnum+=remainder*pow(2,i);
i++;
cout << "decimal is" << " "<< decnum+n<<
endl;
}
}
if (userChoice == 2)
{
long n;
cout << "enter decimal number" << endl;
cin>>n;
int bin=0, i=0, remainder;
while(n!=0)
{
remainder=n%2;
n=n/2;
bin+=remainder*pow(10,i);
i++;
cout << " binary is" << " "<< bin+n <<
endl;
}
}
cout <<"convert another value y or n" << endl;
cin >> again;
}while(again=="y");
}
Sol.
#include <bits/stdc++.h>
#include <string>
#include<cmath>
#include<string>
using namespace std;
int main()
{
string again;
do {
int userChoice;
cout << "Press 2 for Decimal to Binary"<< endl;
cout << "Press 1 for Binary to Decimal: "<<endl;
cout << "Press 3 for Binary to Hexadecimal:
"<<endl;
cout << "Press 4 for Hexadecimal to Binary:
"<<endl;
cin >> userChoice;
if(userChoice == 4){
string hexadec;
cin>>hexadec;
int i=0;
while (hexadec[i]) {
switch (hexadec[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 "
<< hexadec[i];
}
i++;
}
}
if(userChoice == 3){
int bin;
cout<<"Enter Binary Number";
cin>>bin;
int num = bin;
int dec_value = 0;
int base = 1;
int temp = num;
while (temp) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base;
base = base * 2;
}
temp = 0;
int n = dec_value;
char hexaDeciNum[100];
int i = 0;
while(n!=0)
{
temp = n % 16;
if(temp < 10)
{
hexaDeciNum[i] = temp + 48;
i++;
}
else
{
hexaDeciNum[i] = temp + 55;
i++;
}
n = n/16;
}
for(int j=i-1; j>=0; j--)
cout << hexaDeciNum[j];
}
if (userChoice == 1)
{
long n;
cout << "enter binary number" << endl;
cin>>n;
int decnum=0, i=0, remainder;
while(n!=0)
{
remainder=n%10;
n=n/10;
decnum+=remainder*pow(2,i);
i++;
cout << "decimal is" << " "<< decnum+n<<
endl;
}
}
if (userChoice == 2)
{
long n;
cout << "enter decimal number" << endl;
cin>>n;
int bin=0, i=0, remainder;
while(n!=0)
{
remainder=n%2;
n=n/2;
bin+=remainder*pow(10,i);
i++;
cout << " binary is" << " "<< bin+n <<
endl;
}
}
cout <<"\nconvert another value y or n" << endl;
cin >> again;
}while(again=="y");
}
plzzzzzz upvote i need it....