While Loop to divide by 2 continiously
I'm working on a program that takes a number that is entered, then tells you how many times that number can be divided by 2 before it becomes less that 1. If a user enters a negative number it should tell the user that their input was invalid. This is what I have so far, but it isn't taking the count and increasing it every time. So what is wrong with my loop?
Code java:
package project6a;
import java.util.Scanner;
public class Bits {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner (System.in);
System.out.println("Enter a positive number: ");
int userNumber = input.nextInt();
int count = 0;
int tempNum = userNumber / 2;
while (tempNum >= 0){
tempNum = tempNum / 2;
count++;
System.out.println("The number " + userNumber + " is divisible by two " + count + " times");
}
if (userNumber < 0)
System.out.println("Invalid");
input.close();
}
}
Re: While Loop to divide by 2 continiously
Duplicate post -- closed.