Question

In C++ 11, create a program does the following 4 conversions: From Binary to Decimal; From...

In C++ 11, create a program does the following 4 conversions:

From Binary to Decimal;
From Decimal to Binary;
From Hexadecimal to Binary;
From Binary to Hexadecimal;

First create a menu so that user can choose which conversion they wanna do;

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create a flowgorithm program to calculate the Area of Circle first then do the Circumference of...
Create a flowgorithm program to calculate the Area of Circle first then do the Circumference of Circle. The user will have the ability to convert Area OR the Circumference. That means we need to add a decision structure to the program. Furthermore, they need to be able to do it as many times as they want. That means we need to add a main loop structure to the program. The user will also have the choice of whether to run...
EE 263 Homework Assignment 1    Perform the following conversions: Convert 51 (decimal) to binary and...
EE 263 Homework Assignment 1    Perform the following conversions: Convert 51 (decimal) to binary and to hex Convert 0xD1 (hexadecimal) to binary and to decimal Convert 0b11001001 (binary) to hex and to decimal Find the 2’s complement of the following 4-bit numbers 1101 0101 Perform the following 4-bit unsigned operations. For each, indicate the 4-bit result and the carry bit, and indicate if the answer is correct or not. 5 + 8 2 + 12 13 – 7 4...
Convert the hexadecimal number DEADBEEF to binary. Convert the following decimal number to binary: a) 356...
Convert the hexadecimal number DEADBEEF to binary. Convert the following decimal number to binary: a) 356              b) 658 What decimal value does the 8-bit binary number 10011110 represent if it is on a computer using two’s complement representation? What decimal value does the 8-bit binary number 10110100 have if it is on a computer using two’s complement representation? please show your work for question number 4. thank you.
Write a program that reads a binary string (string of 0’s and 1’s) converting the binary...
Write a program that reads a binary string (string of 0’s and 1’s) converting the binary value into decimal. Allow the user to type in as many numbers as they want (one at a time) and end the program when they type in “0”. Use Horner’s method (given in step 3, below) to convert from binary to decimal. Sample Run Binary to Decimal Conversion Enter a binary string: 1101 1101 = 13 decimal Enter a binary string: 10011001 10011001 =...
2 Convert each of the following octal numbers to 10-bit binary, hexadecimal, and decimal. Show your...
2 Convert each of the following octal numbers to 10-bit binary, hexadecimal, and decimal. Show your work. (a) 368 (b) 7568 3 Convert the following binary values into Decimal, Octal and hexadecimal. Show your work. (a) 111010101011112 (b) 1010111011001102 (c) 1011101010001112 (d) 1111101011102 4 Convert the following hexadecimal numbers to 16-bit binary and decimal numbers. Show your work. (a) FE9816 (b) FCAD16 (c) B00C16 (d) FEDF16 5 Perform the addition on the following unsigned binary numbers. Indicate whether or not...
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal....
Write a MIPS Assembly Language program which runs interactively to convert between decimal, binary, and hexadecimal. 1. Request input data type. 2. Request input data. 3. Request output data type. 4. Output the data. The suggested approach was to take the input data as a string. But I am really lost as to how to process the string and then converting it to another data type. Thanks for the help!
Using a loop (not the output format character %o), create a program that takes a positive...
Using a loop (not the output format character %o), create a program that takes a positive integer n, and then displays the polynomial for the octal representation of that integer. Use successive division, as demonstrated in the binary conversion example from the lesson, to do this. For example, for n = 157, the program should output the following: 157 = + (5 * 8^0) + (3 * 8^1) + (2 * 8^2) When this part is working properly, “surround” this...
Create a C# application You are to create a class object called “Employee” which included eight...
Create a C# application You are to create a class object called “Employee” which included eight private variables: firstN lastN dNum wage: holds how much the person makes per hour weekHrsWkd: holds how many total hours the person worked each week. regHrsAmt: initialize to a fixed amount of 40 using constructor. regPay otPay After going over the regular hours, the employee gets 1.5x the wage for each additional hour worked. Methods:  constructor  properties  CalcPay(): Calculate the regular...
Create a program using Binary Trees in Java to do the following 1. Verify a given...
Create a program using Binary Trees in Java to do the following 1. Verify a given expression is balanced in regards to parentheses 2. covert an infix expression to a postfix expression 3. Evaluate the expression Given expressions: String s[] = {"5 + ) * ( 2", " 2 + ( - 3 * 5 ) ", "(( 2 + 3 ) * 5 ) * 8 ", "5 * 10 + ( 15 - 20 ) ) - 25",...
(Use C++ language) Create a program, named threeTypesLoops.cpp, that does the following; 1. Uses a For...
(Use C++ language) Create a program, named threeTypesLoops.cpp, that does the following; 1. Uses a For Loop that asks for a number between 1 and 10; Will double the number each time through the loop and display the answer. Will run five times, so the number will have been doubled five times, and the answer displayed five times. After the loop, display a message saying 'It's done!'. 2. Uses a While Loop; Ask the user to input a friend's name....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT