# Factorial of a Number Code

• 11-28-2013, 09:07 PM
SusieM726
Factorial of a Number Code
I'm a beginning JAVA student and need help. My code asks for input but then I can't get output. Can someone please tell me where I went wrong?

import java.util.Scanner;

class Factorial
{
public static void main(String args[])
{
int n = 0, c, fact = 1;

System.out.println("Welcome to the Factorial Calculator");
System.out.println("Enter an integer that's greater than 0 and less than 10: ");
Scanner in = new Scanner(System.in);

String choice = "y";
while (choice.equalsIgnoreCase("y"))

n = in.nextInt();

if ( n < 0 )
System.out.println("Number should be non-negative.");
else
{
for ( c = 1 ; c <= n ; c++ )
fact = fact*c;

System.out.println("Factorial of "+n+" is = "+fact);
}
}
}
• 11-28-2013, 09:27 PM
Norm
Re: Factorial of a Number Code
[code]
[/code]
to get highlighting and preserve formatting.

Is the program stuck in a loop? Look at the loops in the program to see where it could be looping forever.

HINT: If the value tested in the loop's condition expression is not changed inside the loop, the loop will go forever.

You should ALWAYS use {}s to enclose the code inside loops and following if statements.
Code:

```  while(the condition) {     some code here   }  // end while```
• 11-29-2013, 03:09 AM
SusieM726
Re: Factorial of a Number Code
Sorry Norm - don't know how to edit that post.

No it's not in a loop - there is no output.
• 11-29-2013, 03:16 AM
Norm
Re: Factorial of a Number Code
Can you copy the contents of the command prompt window from when you execute the program and paste it here.

Add the {}s I suggested and post the new version of the program. Be sure to wrap the code in code tags.

Quote:

No it's not in a loop
Can you continue entering numbers to the program as it executes?
• 11-29-2013, 05:21 AM
jim829
Re: Factorial of a Number Code
Part of the problem is your formatting and lack of {}. What code is subject to the while statement?

Regards,
Jim