C++ Convert number systems

Exercise 1

Enter a decimal number, convert it to binary number

Example:


Enter a decimal number: 10
Binary number is 1010
	

Base code:

#include <iostream>
using namespace std;
int main() {
	int num;
	cout << "Enter a decimal number: ";
	cin >> num;

	// convert num to binary number
	// one possible way is to find the largest power of 2 that is less than num, then find the next largest power of 2 that is less than num, and so on
	// eg. 10 = 8 + 2
	// cout 1 for 8, 0 for 4, 1 for 2, 0 for 1




}

Exercise 2

Convert a binary number to decimal number

Example:

Enter a binary number: 10101
Decimal number is 21

Base code:

#include <iostream>
#include <string>
using namespace std;
int main() {
	string binary;
	cout << "Enter a binary number: ";
	cin >> binary;

	// convert binary to decimal number
	// one possible way is to start from the rightmost digit, multiply by 2^0, then multiply by 2^1, and so on
	// eg. 10101 = 1*2^0 + 0*2^1 + 1*2^2 + 0*2^3 + 1*2^4


}