logo CodeStepByStep logo

showTwos

Language/Type: C++ loops while loops parameters %

Write a function named showTwos that shows the factors of 2 in a given integer. For example, the following calls:

showTwos(7);
showTwos(18);
showTwos(68);
showTwos(120);

should produce this output:

7 = 7
18 = 2 * 9
68 = 2 * 2 * 17
120 = 2 * 2 * 2 * 15

The idea is to express the number as a product of factors of 2 and an odd number. The number 120 has 3 factors of 2 multiplied by the odd number 15. For odd numbers (e.g. 7), there are no factors of 2, so you just show the number itself. Assume that your function is passed a number greater than 0.

Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.