Question: C++ Programming Decimal-to-Binary Converter Write a function which converts a decimal number (an int variable) to a string of 0s and 1s representing the same
C++ Programming
Decimal-to-Binary Converter
Write a function which converts a decimal number (an int variable) to a string of 0s and 1s representing the same number in binary (base 2). For example, define an int variable and initialize it to (decimal) 100. Your function, when passed this variable, should return the string "1100100".
Use only arithmetic operators such as multiplication, integer divide /, and the modulo % operators in this exercise. Do NOT use packaged functions to perform conversions or raise numbers to a power.
Algorithm:
- Prompt the user to enter a decimal number and store in an int variable (sentinel is -1)
- Create an empty string to represent that number in base 2
- Using a loop:
- divide the number by two and determine the remainder (modulo). It will be either 0 or 1.
- add the modulo as a character to the end of the string.
- continue dividing the number by two until the result is 0.
- When the loop is finished, print the resulting string
- Go back to step 1 and prompt for the next number to convert
Example: To convert the decimal number 100 to a string of 0's and 1's, continuously take the modulo (remainder) of the number when divided by 2, and put those 0's and 1's together to form a string:
100 / 2 is 50, and 100 % 2 is 0 50 / 2 is 25 and 50 % 2 is 0 25 / 2 is 12 and 25 % 2 is 1 12 / 2 is 6 and 12 % 2 is 0 6 / 2 is 3 and 6 % 2 is 0 3 / 2 is 1 and 3 % 2 is 1 0 and 1 % 2 is 1
The loop stops when the division by 2 results in a 0.
The result is the series of modulo numbers put together to form a string reading up from the bottom: 1100100.
Test your function by computing and printing the string equivalents of 0s and 1s for the following decimal numbers: 0, 5, 32, 240, and 682.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
