logo CodeStepByStep logo

showTwos

Language/Type: Java loops while loops parameters %

Write a method named showTwos that shows the factors of 2 in a given integer. For example, the following calls should produce the following console output:

Call Output
showTwos(7); 7 = 7
showTwos(18); 18 = 2 * 9
showTwos(68); 68 = 2 * 2 * 17
showTwos(120); 120 = 2 * 2 * 2 * 15
showTwos(-68); -68 = 2 * 2 * -17

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. Notice that your method might be passed a negative number.

Method: Write a Java method as described, not a complete program or class.

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.