Write a C++ program using recursion to convert a number in binary (from 1 to 10) to a decimal number. The algorithm for this conversion states that each successive digit in the number is multiplied by the base (2) raised to the power corresponding to its position in the number. The low-order digit is position 0. We sum together all of these products to get the decimal value. For example, if we have the binary number 111001, we convert it to decimal as follows:
A recursive formulation of this algorithm can extract each digit and compute the corresponding power of the base by which to multiply. Once the base case (the last digit is extracted), the function can sum the products as it returns.
In C++, we can represent a binary number using integers. However, there is a danger— the user can enter an invalid number by typing a digit that’s unrepresentable in the base. For example, the number 1011201 is an invalid binary number because 2 isn’t allowed in the binary number system. Your program should check for invalid digits as they are extracted, handle the error in a way that is appropriate (that is, it shouldn’t output a numerical result), and provide an informative error message to the user.
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.