Answered step by step
Verified Expert Solution
Question
1 Approved Answer
C++. Write a function to transform a decimal number into a binary number. The function must receive a positive integer value as a parameter and
C++. Write a function to transform a decimal number into a binary number. The function must receive a positive integer value as a parameter and must return a string with the binary representation of the number.
Given the integer value 7, the return value of your function must be the string: 111
Iterative process to convert a decimal number to binary format:
- Divide the number by 2; the remainder is the first digit of your binary number (least significant). Example: 7%2 = 1
- Subtract the remainder from your number and divide what's left by 2. Example: (7 - (7%2))/2 = 3
- Repeat and concatenate new digits to the front of the binary number. 3%2 = 1 --> 11
Hint: you can use the function to_string (string to_string (int val)) to transform an integer value into a string.
1 #includeStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started