Question

In: Computer Science

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

Solutions

Expert Solution

Code

#include <iostream>
#include <string>

using namespace std;

string decimaltobinary(int x)
{
   string binary = "";
   while (x)
   {
       binary = ((x % 2) ? "1" : "0") + binary;
       x =x / 2;
       if (binary.length() == 4)
           binary = " " + binary; // Formatting
   }
  
   // Formatting
   while (binary.length() != 9)
   {
       binary = "0" + binary; // Adjusting length as 8 bit binary
       if (binary.length() == 4)
           binary = " " + binary; // Formatting
   }

   return binary;
}

string decimaltohex(int x)
{
   string hex = "";
   while (x)
   {
       int rem = x % 16; // taking remainder
       string temp;
       if (rem < 10)
           temp = to_string(rem);
       else
       {
           switch (rem)
           {
           case 10:
               temp = "A";
               break;
           case 11:
               temp = "B";
               break;
           case 12:
               temp = "C";
               break;
           case 13:
               temp = "D";
               break;
           case 14:
               temp = "E";
               break;
           case 15:
               temp = "F";
               break;
           }
       }
       hex = temp + hex;
       x = x / 16;
   }

   while (hex.length() != 2)
       hex = "0" + hex; // Adjusting length as 2 hex

   return hex;
}

string decimaltobcd(int x)
{
   string bcd = "";
   while (x)
   {
       int rem = x % 10; // taking remainder
       string temp;
       switch (rem)
       {
       case 0:
           temp = "0000";
           break;
       case 1:
           temp = "0001";
           break;
       case 2:
           temp = "0010";
           break;
       case 3:
           temp = "0011";
           break;
       case 4:
           temp = "0100";
           break;
       case 5:
           temp = "0101";
           break;
       case 6:
           temp = "0110";
           break;
       case 7:
           temp = "0111";
           break;
       case 8:
           temp = "1000";
           break;
       case 9:
           temp = "1001";
           break;
       }

       bcd = temp + " " + bcd;
       x = x / 10;
   }

   while (bcd.length() != 15)
       bcd = "0000 " + bcd; // Adjusting length as 8 bit binary

   return bcd;
}

int main()
{
   int i = 0;
   cout << "DECIMAL \t BINARY \t HEXADECIMAL \t\t BCD"<<endl;
   while (i < 256)
   {
       cout << i << "\t\t" << decimaltobinary(i) << "\t\t" << decimaltohex(i) << "\t\t" << decimaltobcd(i) << endl;
       i++;
   }

   return 0;
}

Test Output


Related Solutions

2. Write a program that asks for hexadecimal number and converts it to decimal. Then change...
2. Write a program that asks for hexadecimal number and converts it to decimal. Then change it to convert an octal number to decimal in perl language.
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
Convert the following binary values to hexadecimal and decimal (1 pt each) Write Hex Numbers as...
Convert the following binary values to hexadecimal and decimal (1 pt each) Write Hex Numbers as 0x##(ex 0x0A, 0xFF) Binary Hexadecimal Decimal 0001-1011 0x 0000-1000 0000-0100 0000-1001 0001-1111 1001-1001 0111-1010 1100-0010 1110-0101 1000-1010 0011-0100 0001-1001 0100-0011 1111-1111 1110-0111 0001-0010 0100-1000 0100-1110 1001-0001 0110-1100 Name: Convert the following hexadecimal values to binary and decimal Write binary numbers as 0000-0000 Hexadecimal Binary Decimal 0xf1 0xac 0x56 0x6c 0x32 0x30 0x05 0x28 0xf0 0x07 0x42 0xb9 0x6d 0x2f 0x71 0x0e 0x2d 0xfb 0xba...
Program that does conversions (i.e. decimal to hexadecimal, binary to decimal) but needs to have a...
Program that does conversions (i.e. decimal to hexadecimal, binary to decimal) but needs to have a drop down menu to give you the option to choose with conversion you want to go from and convert into (JOptionPane). Cannot use the conversion tool that is built-in Java. Need to use equations or some sort. Please include comments on what is happening to understand.
C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This...
C++ only Write a function decimalToBinaryRecursive that converts a decimal value to binary using recursion. This function takes a single parameter, a non-negative integer, and returns a string corresponding to the binary representation of the given value. Your function should be named decimalToBinaryRecursive Your function should take a single argument An integer to be converted to binary Your function should not print anything Your function should use recursion instead of a loop. Note that if you try to use a...
Write a user-defined MATLAB function that converts real numbers in decimal form to binary form. Name...
Write a user-defined MATLAB function that converts real numbers in decimal form to binary form. Name the function b = deciTObina (d), where the input argument d is the number to be converted and the output argument b is a 30-element-long vector with 1s and 0s that represents the number in binary form. The first 15 elements of b store the digits to the left of the decimal point, and the last 15 elements of b store the digits to...
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
Develop a python program to convert two decimal numbers (A and B) to binary numbers. You...
Develop a python program to convert two decimal numbers (A and B) to binary numbers. You should generate B complement signal (flip all the bits of Input B,
3. Write a java method that accepts a binary number and converts it to decimal then...
3. Write a java method that accepts a binary number and converts it to decimal then display the result. For Example: (110)2 = (6)10 (2 2 *1)+ (21 *1) + (20*0) = 6 Additional task: write a method that accepts a decimal and converts it to binary. i need to solve it as soon as and i will upvote you directly
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT