I need a program that will return an integers factorial, but only when the number entered is between 0 and 20. I have the code to disregard anything below 0 and above 20, but something with my math and/or syntax is off and I dont fully comprehend how to fix the error. My code is below....

import java.util.Scanner;

public class Factorial {

    public static void main(String[] args) {
        int num; // number to find factorial of
        long fac; // factorial so far
        Scanner scan = new Scanner(System.in);

        // Get value to compute factorial of
        System.out.print("Please enter a non-negative integer: ");
        num = scan.nextInt();

        // Compute and print factorial
        // your code here...
        while(num < 0 || num > 20){
            System.out.println("Factorial is undefined for negative or large numbers");
            System.out.print("Please enter an integer number between 0 and 20: ");
            num = scan.nextInt();
        }
//        while(num > 0 || num < 20) {
//            for (int i = 1; i < 50; i ++){
//            fac = num(i) * num(i -1);
//            }
for (int i = 1 ; i <= 20; i++){
num=num - 1 * i;
}
System.out.println("The factorial is: " + num);

    }
  }
}

in your loop at line 25 you dont need to iterate it 20 times but you use the input instead, use another variable to store the factorial

So you are saying where I have I <= 20, I should have my user input as "num <= 20" and use the variable fac as my factorial variable?

you need to update the code at line25,26,27,28 as

if(num ==0)
{
 fac = 1;
}
else{
 for(int i=1;i<=num;i++){
  fac = fac*num
 }
}
System.out.println("The factorial is: " + fac);

``

@piyush
Please don't just spoon feed raw code to people. All that teaches them is how easy it is to cheat with a quick copy/paste. You didn't even attempt to explain why you made the changes you did. Give people assistance that helps them learn how to fix problems for themselves.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.