173k views
3 votes
/*

* Write a method called showTwos that shows the factors of 2 in a given integer.
* For example, consider the following calls:
showTwos(7);
showTwos(18);
showTwos(68);
showTwos(120);
These calls should produce the following output:
7 = 7
18 = 2*9
68 = 2*2*17
120 = 2*2*2*15
* */

User EpiMan
by
5.9k points

1 Answer

3 votes

Answer:

The answer to this question can be given as:

Method:

public static void showTwos(int number) //method definition.

{

method body.

System.out.print(number + " = "); //print number

while (number % 2 == 0) //condition

{

System.out.print("2 * "); //print message.

number = number / 2;

}

System.out.println(number); //print value.

}

Explanation:

In the above method definition firstly, we declare the method showTwos() that name is already given in the question then we pass a variable number as a parameter in the method. In this first, we take input number from the user and pass into this method. In this method we use a while loop it is an entry control loop in this loop first we modules the number if it is equal to 0.Then we divide the number by 2 and in the last, we print the value.

User Fireant
by
6.4k points