logo CodeStepByStep logo

show_twos

Language/Type: PHP loops while loops parameters mod

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

show_twos(0);
show_twos(5);
show_twos(18);
show_twos(68);
show_twos(-68);
show_twos(120);

should produce this output:

0 = 0
5 = 5
18 = 2 * 9
68 = 2 * 2 * 17
-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.

If passed a negative integer, the only negative multiple in the result should be the last multiplied value, as shown in the above example for show_twos(-68).

You may assume that the value passed is an Integer type.

Function: Write a PHP 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.