Ask Question
28 August, 04:49

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:

+3
Answers (1)
  1. 28 August, 04:50
    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?
Find 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 the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers