Question

In: Computer Science

Write a c++ program to convert any decimal number to either binary base  or Hex base...

Write a c++ program to convert any decimal number to either binary base  or Hex base number system. Test your program with the followings:


Convert 15 from decimal to binary. 
Convert 255 from decimal to binary.
Convert BAC4 from hexadecimal to binary
Convert DEED from hexadecimal to binary. 
Convert 311 from decimal to hexadecimal.
Convert your age to hexadecimal.


Solutions

Expert Solution

// C++ program to convert Decimal number to binary or hexadecimal

#include <iostream>

#include <sstream>

using namespace std;

int main() {

       int decimalNumber;

       string convertedOutput;

       int choice;

       // input the decimal number

       cout<<"Enter the decimal number to convert :";

       cin>>decimalNumber;

       // input the choice of conversion

       cout<<"Enter the choice of conversion : 1. Binary 2. Hexadecimal : ";

       cin>>choice;

       cout<<"Decimal : "<<decimalNumber;

       convertedOutput = "";

       int rem;

       int temp = decimalNumber;

       if(choice == 1) // convert decimal to binary

       {

             // loop that continues till temp > 0

             while(temp > 0)

             {

                    rem = temp%2; // get the remainder left after multiplying temp by 2

                    temp = temp/2; // get the value left after multiplying temp by 2

                    // convert the rem to string

                    stringstream ss;

                    ss<<rem;

                    // add the rem to output

                    convertedOutput = ss.str() + convertedOutput;

             }

             cout<<" Binary : ";

       }else // convert decimal to hexadecimal

       {

             // loop that continues till temp > 0

             while(temp > 0)

             {

                    rem = temp%16; // get the remainder left after multiplying temp by 16

                    temp = temp/16; // get the value left after multiplying temp by 16

                    // if rem < 10, then add the string equivalent to output

                    if(rem < 10)

                    {

                           stringstream ss;

                           ss << rem;

                           convertedOutput = ss.str() + convertedOutput;

                    }else // if rem >=10

                    {

                           // get the corresponding letter of the hexadecimal and add it to output

                           convertedOutput = (char)('F'-(15-rem)) + convertedOutput;

                    }

             }

             cout<<" Hexadecimal : ";

       }

       // output the converted value

       cout<<convertedOutput<<endl;

       return 0;

}

//end of program

Output:

Convert 15 from decimal to binary.

Convert BAC4 from hexadecimal to binary (decimal equivalent of BAC4 = 47812)

Convert 311 from decimal to hexadecimal.


Related Solutions

c ++ program that converts from any base to a decimal number
c ++ program that converts from any base to a decimal number
Write a program to convert the input numbers to another number system. 1. Decimal to Binary...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary 2. Binary to Decimal 3. Hexadecimal to Decimal 4. Decimal to Hexadecimal 5. Binary to Hexadecimal 6. Hexadecimal to Binary The user will type in the input number as following: Binary number : up to 8 bits Hexadecimal number: up to 2 bytes Decimal number: Less than 256 As a result, print out the output after the conversion with their input numbers. The program...
How to convert binary to hex?
How to convert binary to hex?
Convert the following unsigned numbers to the requested form: 01100001 binary to: hex, and also decimal...
Convert the following unsigned numbers to the requested form: 01100001 binary to: hex, and also decimal Hex: Decimal: b) 136 decimal to: hex, and also binary Hex: Binary:
Convert the decimal number, 315.56 into binary form?
Convert the decimal number, 315.56 into binary form?
Code in C-language programming description about convert binary number to decimal number.
Code in C-language programming description about convert binary number to decimal number.
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the...
Python program. Write a python program that can convert any radix-d (arbitrary base) number to the equivalent radix-e (another arbitrary base) number. Where e and d are members in [2, 16]. Remember, base 16 needs to be calculated as hexadecimal. So, if radix-d is input as a hexadecimal number, it needs to convert and output to desired base. Conversely, if base 16 is the desired output, then the output needs to show a hexadecimal number. Hints: The easiest approach is...
Convert the following binary number to dotted decimal format. 11000000100110010000100101011001
Convert the following binary number to dotted decimal format. 11000000100110010000100101011001
1.Write a Java program that inputs a binary number and displays the same number in decimal....
1.Write a Java program that inputs a binary number and displays the same number in decimal. 2.Write Java program that inputs a decimal number and displays the same number in binary.
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. Use any algorithm
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT