In this lab, we focus on practicing the use of string type and loops. You will design and implement a program that implements the conversion from base 10 (decimal) to binary.
Requirement Your program asks the user to enter a number in decimal representation, and then the program should display the number's binary representation.
$convertPlease recall the procedure explained in the youtube video. You can use the following function to calculate the exponents:
Welcome to the base converter by X. Zhang!
Pelase enter the number:34
34 is 10010 in base 2.
pow (2,8) will give you 2^8 (2 raised to the power of 8) pow (2,i) will give you 2^i, for any given value of iNote that the function gives you a double type, and you should convert it to int:
(int)pow(2,8)Finally, to use the above pow function, you need to include one more header files in your program:
#include <iostream> #include <math.h> using namespace std; int main() { ... }
Extra credits: You earn extra credit points for supporting other conversion such as the following:
How would you support the conversion between base 10 and any base smaller than 10 (base 2, 3, 4, ..., 9)?
$ convert
Welcome to base converter by X. Zhang!
Please enter the number:29
Please enter the target base:8
29 is 35 in base 8. Example output 3
$ convert
Welcome to base converter by X. Zhang!
Please enter the number:31
Please enter the target base:16
31 is 1F in base 16.