In: Computer Science
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 = 40:
20 10 5 2 1
In java
import java.util.Scanner;
public class DivideByTwoLoop {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
int userNum;
userNum = scnr.nextInt();
*insert code*
System.out.println("");
}
}
'
import java.lang.*;
import java.util.Scanner;
public class dvd {
public static void main (String [] args) {
Scanner scnr = new
Scanner(System.in);
int userNum;
userNum = scnr.nextInt();
while(userNum!=1){ //iterates
the loop until userNum is 1
userNum=userNum/2; //userNum = userNum divided by 2
System.out.print(userNum+" "); //prints userNum.print for same
line
}
System.out.println("");
}
}