Program 4 A decimal to hexadecimal converter program /* * The application program to change a decimal number to its * hexadecimal equivalent. */ #include "stack.cpp" int main () { // Instantiation of a Stack object Stack <char> stack; // Instantiation of two string objects and two integer variables string converter ("0123456789ABCDEF"); string hexadecimal; int decimal; int index; // Input the decimal number do Program 4 A decimal to hexadecimal converter program (continued) { cout << "Enter a positive integer: "; cin >> decimal; } while (decimal <= 0); cout << "The decimal number: " << decimal << endl; // Creation of hexadecimal characters and push them into stack while (decimal != 0) { stack.push (converter[decimal % 16]); decimal = decimal / 16; } // Pushing the characters from the stack into the hexadecimal string while (!stack.empty()) { hexadecimal.push_back (stack.top()); stack.pop (); } // Printing the hexadecimal number cout << "The hexadecimal number: " << hexadecimal; return 0; } Run: Enter a positive integer: 124 The decimal number: 124 The hexadecimal number: 7C Run: Enter a positive integer: 1345 The decimal number: 1345 The hexadecimal number: 541 Run: Enter a positive integer: 11 The decimal number: 11 The hexadecimal number: B
Added by Jes-S G.
Close
Step 1
Step 1: Create a Stack object of type char to store the binary digits. Show more…
Show all steps
Your feedback will help us improve your experience
Supreeta N and 52 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Assignment – Decimal to Octal conversion program Given a decimal number as input, we need to write a program to convert the given decimal number into equivalent octal number. Convert the number with base value 10 (Decimal) to base value 8 (Octal). The base value of a number system determines the number of digits used to represent a numeric value. For example, the binary number system uses two digits 0 and 1, octal number system uses 8 digits from 0-7 and decimal number system uses 10 digits 0-9 to represent any numeric value. Example: 33 (Base-ten (Decimal)) = 41 (Base-eight (Octal)) Your program will prompt a Decimal value (any value) and convert the Decimal value to an Octal value. Sample Input: Enter a decimal value (Base-ten): _ Sample Output: Decimal value = 33 Octal value = 41 Algorithm: If the given decimal number is 33. Step 1: Remainder when 33 is divided by 8 is 0. Therefore, Push the value 0 in the stack. Step 2: Divide 33 by 8. New number is 33/8 = 4. Step 3: Remainder when 4 is divided by 8 is 2. Therefore, Push the value 4 in the stack. Step 4: Divide 4 by 8. New number is 4/8 = 0. Step 5: Since number becomes = 0. Stop repeating steps and Pop the item from stack and display until the stack is EMPTY.
Supreeta N.
In C or C++ You should complete the above program under the name of a project COMP2650_Lab03_{UWinID} that firstly outputs a menu of commands as follows: Enter the command number: 0) Exit 1) AND 2) OR 3) NOT 4) l's complement 5) 2's complement 6) 2's complement* Based on the user's chosen number of commands, the program should then ask for the input(s). After that, the program asks to what base the user wants to see the results. Then, it applies the command and prints out the result in the requested base. For instance, if a user selects (1), the program should accept two inputs as follows: Enter the first binary number: x0 x1 = x7 Enter the second binary number: y0 y1 = y7 When the user enters the two binary numbers, the program asks for a base number to print out the result: Enter the output base: 1) Binary 2) Octal 3) Decimal 4) Hexadecimal Then the program applies the AND command on the input x and y and prints the result on the selected base and comes back to the main menu. Other commands should follow the same flow. If the user selects (0), the program ends. Please restrict the user to enter inputs within the range {0,1}. For instance, if the user enters 2, -1, print out an error message and come back to ask for correct inputs. It is required to write a modular program according to the following instructions: 1. Reorganize the previous functions in Lab02 in separate files as I did in this manual. 2. For the base conversion, create convert_tools.cpp and convert_tools.h. 3. Write functions in convert_tools.cpp to output the result according to the selected base by the user by calling the functions from main.cpp file: void to_octal(int a[]){} void to_decimal(int a[]){} void to_hexadecimal(int a[]){} For converting binary numbers to octal or hexadecimal, you can use either the steps explained in the class or the fast method explained in assignment Lec01. For converting to decimal, you can use the sum of powers of 2, as described in the class. #include #include "logic_tools.h" #include "comp_tools.h" #define MAX 8 // Byte = 8 bits int main(void) { setbuf(stdout, NULL); int x[MAX]; int y[MAX]; printf("Enter the first binary number: "); for(int i=0; i < MAX; i++) { scanf("%d", &x[i]); } printf("Enter the second binary number: "); for(int i=0; i < MAX; i++) { scanf("%d", &y[i]); } printf("Enter the output base: 1) Binary 2) Octal 3) Decimal 4) Hexadecimal "); int base; scanf("%d", &base); // Apply the selected command and print the result in the requested base switch(command) { case 1: // Apply AND command break; case 2: // Apply OR command break; case 3: // Apply NOT command break; case 4: // Apply l's complement command break; case 5: // Apply 2's complement command break; case 6: // Apply 2's complement* command break; default: printf("Invalid command number. Please try again. "); break; } return 0; }
Akash M.
Write a program that will demonstrate some of the C++ Library Functions, Stream Manipulators, and Selection Control Structures. The user will be given a menu of four choices. They can input a 1 for finding Cosines, 2 for finding Logarithms, 3 for converting between Decimal and Hexadecimal, or 4 to Exit the program. You must use the proper functions and/or stream manipulators to find the answers. If the user picks the cosine, ask them if they want to find the cosine, arc cosine, or hyperbolic cosine. Then input a floating point number (in radians) and find and print to three decimal places the proper type of cosine. If the user picks the logarithms, ask them if they want to find the common logarithm or the natural logarithm. Then input a floating point number and find and print to three decimal places and a plus or minus sign (for positive or negative numbers) the proper type of logarithm. If the user picks the conversion, ask them if they want to convert decimal to hexadecimal or hexadecimal to decimal. If they pick decimal to hexadecimal, ask them if they want to use lowercase or uppercase letters in the printing of the hexadecimal number. Use boolean input, so input the answer as true or false and put the user's answer in a boolean alphabetic variable. Then input a whole number in the proper base. Print the inputted number in the user's base and in the converted base. Use the prefix for printing the hexadecimal numbers. If the user picks the exit, use the actual exit function to end the program Include comments if you can.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
18,000,000+
Students on Numerade
Trusted by students at 8,000+ universities
Watch the video solution with this free unlock.
EMAIL
PASSWORD