#include using namespace std; unsigned int x, x_backup; int i = 0; // This assignment is worth 5 points. // -1 for forgetting the 0b prefix. // -1 for having extra leading 0s in output. // -2 if math library was used. int main() { cout << "Enter a number: "; cin >> x; x_backup = x; cout << "Binary value is 0b"; while(x > 0) // This loop determines the highest power of 2 needed { i++; x /= 2; // You can also use x >>= 1 instead } x = x_backup; for(i--; i >= 0; i--) { cout << x/(1 << i); x %= (1 << i); } return 0; }