Factorial of a number USING JAVA
A program to accept a number from keyboard and print the Factorial.
import java.util.Scanner;
public class Factorial
{
public static void main(String[] args)
{ final int fact;
System.out.println("Enter a number to find the Factorial");
Scanner in = new Scanner(System.in);
fact=in.nextInt();
System.out.println( "Factorial of " + fact + " is " + factorial(fact));
}
public static int factorial(int n)
{
int result = 1;
for(int i = 2; i <= n; i++)
result *= i;
return result;
}
}
Output:
Enter a number to find the Factorial
5
Factorial of 5 is 120