Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: While Loop to divide by 2 continiously

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Thanked 0 Times in 0 Posts

    Default 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?

    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;	
    			System.out.println("The number " + userNumber + " is divisible by two " + count + " times");
    		if (userNumber < 0)

  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    My Mood
    Thanked 139 Times in 134 Posts

    Default Re: While Loop to divide by 2 continiously

    Duplicate post -- closed.

Similar Threads

  1. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  2. How come 1 divide by 2 is less than 0.5 ?!
    By raabhim in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 1st, 2012, 12:43 PM
  3. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 07:57 PM
  4. How to divide elements in two different arrays
    By BuhRock in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 11th, 2011, 05:38 PM
  5. How can i divide a string?
    By noFear in forum Java Theory & Questions
    Replies: 9
    Last Post: September 1st, 2010, 08:45 AM