# Help with While Loop

Page 1 of 1

## 11 Replies - 5278 Views - Last Post: 26 February 2010 - 01:05 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=158402&amp;s=8375575674519dbcfe2ad3c3b5a75b06&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 justabeginner

Reputation: 0
• Posts: 36
• Joined: 17-February 10

# Help with While Loop

Posted 25 February 2010 - 08:59 PM

I have to write a program to compute the factorial of a non-negative integer using a while loop.

I am not sure if I am stuck in an infinite loop or not, but when I run the code for an integer greater than 1, it allows the user to keep inputing data and does not do anything else. The code does work for an integer less than 1 or equal to zero. Anyone have a clue what I am doing wrong???

This is the code I have written thus far:

import java.util.Scanner;

public class Factorial
{
public static void main (String[] args)
{
int num;
int result=0;
int count = 0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a non-negative integer: ");
num = scan.nextInt();

while (num<0)
{
System.out.println("Cannot enter a negative number.\n" +
"Please enter a non-negative integer: ");
num = scan.nextInt();
}
if (num==0)
{
System.out.println("Zero factorial is 1");
}

while (num>1)
{
count = num - 1;
while (count>1)
{
result = num * (num-1);
count--;
}
}
if (num>0)
{
System.out.println("The factorial of " + num + "is " + result);
}

}

}

Is This A Good Question/Topic? 0

## Replies To: Help with While Loop

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Help with While Loop

Posted 25 February 2010 - 09:05 PM

### #3 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12222
• Posts: 45,296
• Joined: 27-December 08

## Re: Help with While Loop

Posted 25 February 2010 - 09:06 PM

I would try something along the lines of this:
```//given n, the number to find factorial of
public int factorial(int n){

//if the user entered a number <= 1, define n! = 1
if(n <= 1) return 1;

//otherwise, iteratively calculate n!
int fact = n;
n--;
for(;n > 1; n--) //until n == 1
fact *= n;

return fact;
}

```

### #4 justabeginner

Reputation: 0
• Posts: 36
• Joined: 17-February 10

## Re: Help with While Loop

Posted 25 February 2010 - 09:19 PM

I thought about using a for loop, but the assignment asks for a while loop,
just not sure how to get my program to jump out of the loop, because I thought it would when count
reached the value 2 (since there really is no need to multiply the result by 1), but it looks like I
have an infinite loop. Not sure yet if my calculations will return the correct result, but
I cannot check it until I get my loop working.

### #5 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12222
• Posts: 45,296
• Joined: 27-December 08

## Re: Help with While Loop

Posted 25 February 2010 - 09:22 PM

Very easy to make a for loop a while loop. Like so:
```for(;n > 1; n--) fact *= n;

//TO

while(n > 1){
fact *= n;
n--;
}

```

### #6 justabeginner

Reputation: 0
• Posts: 36
• Joined: 17-February 10

## Re: Help with While Loop

Posted 25 February 2010 - 09:39 PM

Well, that got me out of the infinite loop, but for some reason, it is computing zero for everything. I guess because I have my variable initialized to zero, but I get an error without initializing the variable 'fact'.

### #7 javabie

Reputation: 35
• Posts: 117
• Joined: 12-February 10

## Re: Help with While Loop

Posted 25 February 2010 - 09:57 PM

initialize to 1...

if its zero then..
anything * 0 = 0..

### #8 justabeginner

Reputation: 0
• Posts: 36
• Joined: 17-February 10

## Re: Help with While Loop

Posted 25 February 2010 - 10:16 PM

Okay, my print line is reading whatever value I initialize my variable 'fact' to, so my factorial is not computing. All I did was take out my nested while loop from the first post and entered the while loop as posted in #5 (and changed my variable from result to fact). I don't understand how the while loop is returning the product each pass and storing it in the variable 'fact'...or maybe it isn't???

### #9 javabie

Reputation: 35
• Posts: 117
• Joined: 12-February 10

## Re: Help with While Loop

Posted 25 February 2010 - 10:33 PM

try this...

```	import java.util.Scanner;

public class Factorial
{
public static void main (String[] args)
{
int n=1;
int input;
int fact=1;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a non-negative integer: ");
input = scan.nextInt();
while (n<0)
{
System.out.println("Cannot enter a negative number.\n" +
"Please enter a non-negative integer: ");
n = scan.nextInt();
}
if (n==0)
{
System.out.println("Zero factorial is 1\n");
}
while(n <= input){
fact *= n;
n++;
}

System.out.println("The factorial of " + input + "is " + fact);

}

}

```

Edited: before partial code were posted... now full code is posted..

This post has been edited by javabie: 25 February 2010 - 10:35 PM

### #10 Simple_Condolences

Reputation: 11
• Posts: 180
• Joined: 10-January 10

## Re: Help with While Loop

Posted 26 February 2010 - 08:37 AM

```while (n<0)//n needs to be input
{
System.out.println("Cannot enter a negative number.\n" +
"Please enter a non-negative integer: ");
n = scan.nextInt();//n needs to be input
}
if (n==0) //n needs to b input
{
System.out.println("Zero factorial is 1.\n");
}

```

And try this...

```import java.util.Scanner;

public class Factorial
{
public static void main (String[] args)
{
int n=1;
int input;
int fact=1;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a non-negative integer: ");
input = scan.nextInt();
while (input<0)
{
System.out.println("Cannot enter a negative number.\n" +
"Please enter a non-negative integer: ");
input = scan.nextInt();
}
if (input==0)
{
System.out.println("Zero factorial is 1\n");
}
while(n <= input){
fact *= n;
n++;
}

System.out.println("The factorial of " + input + " is " + fact);

}

}

```

- Zach

Reputation:

## Re: Help with While Loop

Posted 26 February 2010 - 12:27 PM

Got my program to work now! Thanks to all of you for helping! I verified that the answers for factorial are accurate, and it seems that it is only correct up to the number 12. Is there a reason for this? Or, is that just as high as Java will go???

### #12 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12222
• Posts: 45,296
• Joined: 27-December 08

## Re: Help with While Loop

Posted 26 February 2010 - 01:05 PM

Yes- overflow and underflow. That is, when you reach the end limits of the int, the values will cycle back around. If you are concerned about this, you could use a long (which will eventually experience the same problem) or a java.math.BigInteger, which will not experience this problem.