Write a C++ program that shows the menu below. User can select an item by insert
ID: 3703898 • Letter: W
Question
Explanation / Answer
#include <iostream>
#include<math.h>
#include<string.h>
#include<string>
using namespace std;
int SignedDecimalToBinary()
{
long decimal = 1;
long remainder = 1;
long i = 1;
long sum = 0;
cout << "Enter Decimal Number: " << endl;
cout << "Decimal: "<< endl;
cin >> decimal;
do
{
//Conversion to Signed Binary.
remainder = decimal %2;
sum = sum + (i * remainder);
decimal = decimal / 2;
i = i * 10;
}while (decimal < 0 || decimal > 0); //Less Than 0 to Make Value Signed.
cout << "Binary: " << sum << endl << endl;
return 0;
}
int binary_decimal(int n) //Function For Signed Binary to Decimal.
{
int decimal = 0, i = 0, remainder;
//Two's Compliment.
//n = ~n + 1;
//Conversion to Decimal.
while (n != 0)
{
remainder = n % 10;
n /= 10;
decimal += remainder * pow(2,i);
++i;
}
return decimal;
}
int SignedBinaryToDecimal()
{
int binary_decimal(int n);
int n;
cout << "Enter Binary Number: ";
cin >> n;
cout << "In Decimal: " << n << " = " << binary_decimal(n) << endl;
return 0;
}
int convertBinaryToDecimal()
{
long n;
cin>> n;
int decimalNumber = 0, i = 0, remainder;
while (n!=0)
{
remainder = n%10;
n /= 10;
decimalNumber += remainder*pow(2,i);
++i;
}
cout << "In Decimal: " << n << " = " << decimalNumber << endl;
return 0;
}
long convertDecimalToBinary()
{
int n;
cin >> n;
long long binaryNumber = 0;
int remainder, i = 1, step = 1;
while (n!=0)
{
remainder = n%2;
cout << "Step " << step++ << ": " << n << "/2, Remainder = " << remainder << ", Quotient = " << n/2 << endl;
n /= 2;
binaryNumber += remainder*i;
i *= 10;
}
cout << "In Binary: " << n << " = " << binaryNumber << endl;
return 0;
}
int DecimalToTwocomplement(){
long decimal = 1;
long remainder = 1;
long i = 1;
long sum = 0;
cout << "Enter Decimal Number: " << endl;
cout << "Decimal: "<< endl;
cin >> decimal;
do
{
//Conversion to Signed Binary.
remainder = decimal %2;
sum = sum + (i * remainder);
decimal = decimal / 2;
i = i * 10;
}while (decimal < 0 || decimal > 0); //Less Than 0 to Make Value Signed.
cout << "Binary: " << sum << endl << endl;
return 0;
}
int main()
{
cout << "1: convertBinaryToDecimal"<< endl;
cout << "2: convertDecimalToBinary"<< endl;
cout << "3: SignedDecimalToBinary"<< endl;
cout << "4: SignedBinaryToDecimal"<< endl;
cout << "5: DecimalToTwocomplement" << endl;
cout << "6: Exit"<< endl;
int input;
while(1>0){
cout << "Enter the Options:" << endl;
cin >> input;
if(input==1){
convertBinaryToDecimal();
continue;
}
if(input==2){
convertDecimalToBinary();
continue;
}
if(input==3){
SignedBinaryToDecimal();
continue;
}
if(input==4){
SignedDecimalToBinary();
continue;
}
if(input==5){
DecimalToTwocomplement();
continue;
}
if(input==6){
cout<< "bye" << endl;
break;
}
}
return 0;
}
Please find the code above
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.