# Thread: Factorial of a Number Code

1. Member
Join Date
Nov 2013
Posts
2
Rep Power
0

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

2. ## 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.
Java Code:
```  while(the condition) {
some code here
}  // end while```
Last edited by Norm; 11-28-2013 at 09:30 PM.

3. Member
Join Date
Nov 2013
Posts
2
Rep Power
0

## 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.

4. ## 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.

No it's not in a loop
Can you continue entering numbers to the program as it executes?

5. Senior Member
Join Date
Jan 2013
Location
United States
Posts
2,685
Rep Power
4

## 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•