Ask Question
13 May, 17:54

In java, Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow each number by a space. Example output for userNum = 20:

+5
Answers (1)
  1. 13 May, 19:44
    0
    public class HelloWorld{

    public static void main (String []args) {

    int userNum = 20;

    while (userNum!=1) {

    userNum = userNum/2;

    System. out. print (userNum+" ");

    }

    }

    }

    Explanation:

    Create the main function and declare the variable userNum with value.

    then, use a while loop and it run until the userNum not equal to one when it become equal to 1 the while loop terminate.

    Inside the loop, divide the userNum by 2 and then assign to userNum and this process continue until condition true.

    for example:

    userNum = 20

    userNum = 20/2=10

    then, 10/2=5

    5/2=1

    after that condition false. and while loop terminate.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “In java, Write a while loop that prints userNum divided by 2 (integer division) until reaching 1. Follow each number by a space. Example ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers