0

what I'm trying to do is use a while loop to check userNum % 2 > 1 once it hits one i want it to stop and print out all the values of the division so for example

if 20 is user num it would generate. 20 / 2 and 10/ 2 and 5/2 and then 2/2 resulting in 1 and then stopping (integer division)

import java.util.Scanner;

public class DivideByTwoLoop {
   public static void main (String [] args) {
      int userNum = 0;

      userNum = 20;

      while ( (userNum % 2) > 1){
         userNum = userNum / 2;
         System.out.println(userNum );
      }

      System.out.println("");

      return;
   }
}

thats what I have so far. any help is greatly appreciated.

Ibrahim
  • 19
  • 1
  • 3

1 Answers1

6

userNum % 2 is always either 1 or 0, so the condition of the while-loop is never true. The condition should be userNum > 1 instead of (userNum % 2) > 1.

  • Thanks for the reply, my issue now is that while my expected output is 20 10 5 2 1. im getting 20 10 4 2. for some reason. i have changed the loop to look like While ( userNum >1 ){ userNum = userNum /2 System.out.print(userNum); – Ibrahim Nov 19 '15 at 19:51
  • @Ibrahim i've tested the code. works fine for me (except for the missing 20 in the output). Is the code in the question the complete example? –  Nov 19 '15 at 21:09
  • it was, but thanks I actually ended up tinkering with it later on after I replied and got it to work, thanks for your assistance though! – Ibrahim Nov 20 '15 at 22:12