Help with While Loop

Cannot get while loop to compute a factorial

Page 1 of 1

11 Replies - 5369 Views - Last Post: 26 February 2010 - 01:05 PM Rate Topic: -----

#1 justabeginner   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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   User is offline

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

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

Re: Help with While Loop

Posted 25 February 2010 - 09:05 PM

Please :code:
That helps us to help you
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12306
  • View blog
  • Posts: 45,403
  • Joined: 27-December 08

Re: Help with While Loop

Posted 25 February 2010 - 09:06 PM

Please, :code:

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;
}


Was This Post Helpful? 0
  • +
  • -

#4 justabeginner   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12306
  • View blog
  • Posts: 45,403
  • 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--;
}


Was This Post Helpful? 0
  • +
  • -

#6 justabeginner   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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'.
Was This Post Helpful? 0
  • +
  • -

#7 javabie   User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • 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..
Was This Post Helpful? 1
  • +
  • -

#8 justabeginner   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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???
Was This Post Helpful? 0
  • +
  • -

#9 javabie   User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#10 Simple_Condolences   User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • 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");
}



Look at the comments.

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
Was This Post Helpful? 0
  • +
  • -

#11 Guest_justabeginner*


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???
Was This Post Helpful? 0

#12 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12306
  • View blog
  • Posts: 45,403
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1