Question

In: Computer Science

code in c++ using the code given add a hexadecimal to binary converter and add a...

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");
}

Solutions

Expert Solution

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....


Related Solutions

Convert a list of decimal numbers into their binary and hexadecimal equivalents Add the elements of...
Convert a list of decimal numbers into their binary and hexadecimal equivalents Add the elements of each of these lists to generate a total sum Print the lists, and the total sum of each value C++ contains some built-in functions (such as itoa and std::hex) which make this assignment trivial. You may NOT use these in your programs. You code must perform the conversion through your own algorithm. The input values are: 5 9 24 2 39 83 60 8...
1. Design and implement a 4 bit binary to excess 3 code converter using CMOS transistors....
1. Design and implement a 4 bit binary to excess 3 code converter using CMOS transistors. (Note: Students are expected to design the circuit with truth table, solve the output expression by use of K Map or suitable circuit Reduction technique and implement using CMOS transistors.)
Convert 110.7510 to binary ______ and hexadecimal ______. Show the answer in both binary and hexadecimal....
Convert 110.7510 to binary ______ and hexadecimal ______. Show the answer in both binary and hexadecimal. There are ____________ kilobytes in a megabyte. Convert -13210 to a 16-bit 2’s complement in binary ____________ and hexadecimal ______________.
write a python code to Determine the binary and decimal representations of the following hexadecimal numbers....
write a python code to Determine the binary and decimal representations of the following hexadecimal numbers. Store answers in the appropriate variables in the .py file. (a) 0xfca1 (b) 0xc4d8
1. Convert to binary and hexadecimal (PLEASE SHOW WORK) a. 35 - binary: - hexadecimal: b....
1. Convert to binary and hexadecimal (PLEASE SHOW WORK) a. 35 - binary: - hexadecimal: b. 85 - binary: - hexadecimal: c. 128 - binary: - hexadecimal: d. 4563 - binary: - hexadecimal:
C++ Write the code to implement a complete binary heap using an array ( Not a...
C++ Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap. Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc Set array size to 31 possible integers. Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13 Have a default constructor that initializes the array to zeros.. The data in the heap will be double datatype. PART 2 Convert to the program to a template, test with integers, double and char please provide screenshots thank you so...
Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are...
Write a program in C++ that converts decimal numbers to binary, hexadecimal, and BCD. You are not allowed to use library functions for conversion. The output should look exactly as follows: DECIMAL      BINARY                     HEXDECIMAL                      BCD 0                      0000 0000                   00                                            0000 0000 0000 1                      0000 0001                   01                                            0000 0000 0001 2                      0000 0010                   02                                            0000 0000 0010 .                       .                                   .                                               . .                       .                                   .                                               . 255                  1111 1111                   FF                                            0010 0101 0101
Design a combinational circuit that implements a Binary-to-Grey Code converter. Your input should be a four-bit...
Design a combinational circuit that implements a Binary-to-Grey Code converter. Your input should be a four-bit binary number, and your output should be the equivalent four-bit Grey Code value. First, design the circuit using NAND gates only. Next, design the circuit using a minimal number of 2-input XOR gates.
6. Add using binary addition 26 and 56 45 and 65      7. Subtract using binary...
6. Add using binary addition 26 and 56 45 and 65      7. Subtract using binary two’s complement 23 from 45 34 from 65
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT