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 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 ______________.
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.)
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...
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.
(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...
Code using C++ A binary search tree is a data structure designed for efficient item insertion,...
Code using C++ A binary search tree is a data structure designed for efficient item insertion, deletion, and retrieval. These 3 operations share an average run time complexity of O(log(n)). Such time complexities are guaranteed whenever you are working with a balanced binary search tree. However, if you have a tree that begins leaning heavily to either its left or right side, then you can expect the performances of the insertion, deletion, and retrieval operations to degrade. As an example,...
in C programming Write the code to traverse the binary tree using in-order, post-order and pre-order...
in C programming Write the code to traverse the binary tree using in-order, post-order and pre-order methods. Make sure to validate that your output is correct. Review the terminology for tree structures and list structures. What is the root of the tree, what’s a parent, what’s a child, what’s an ancestor, what is an external node, what is an internal node, what is the head of the list, what is the tail of the list, etc. Traverse the binary tree...
Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip>...
Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip> #include<algorithm> using namespace std; const int row = 10; const int col = 7;   ifstream name; ifstream grade; ofstream out; void readData(string stname[row], int stgrade[row][col]); void stsum(int stgrade[row][col], int total[row]); void staverage(int total[row], double average[row]); void stlettergrade(double average[row],char ltrgrade[row]); void displayData(string stname[row], int stgrade[row][col], int total[row], double staverage[row], char ltrgrade[row]); void swapltrgrade(char* xp, char* yp); int main() {    int STG[row][col] = { {0},{0}...
Write c code to determine if a binary number is even or odd. If it is...
Write c code to determine if a binary number is even or odd. If it is odd, it outputs 1, and if it is even, it outputs 0. It has to be less than 12 operations. The operations have to be logical, bitwise, and arithmetic.
Convert the following numbers to 8-bit binary and 8-bit hexadecimal: a) 20 b) 78 c) -25...
Convert the following numbers to 8-bit binary and 8-bit hexadecimal: a) 20 b) 78 c) -25 d) -96 Convert the following hexadecimal numbers to binary and decimal assuming two's compliment format: a) 0x56 b) 0x14 c) 0xF8 d) 0xCC MUST DO ALL PROBLEMS AND SHOW ALL WORK!!!!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT