import java.util.*;
class FindFactorial
{
public static void main(String args[])
{
int n;
System.out.println("Enter the number");
Scanner s=new Scanner(System.in);
n=s.nextInt();
// If user enters 0 then 0!=1
long f=1;
for(int i=n;i>=1;i--)
{
f*=i;
}
System.out.println("Factorial of "+n+" is "+f);
}
}
Sample Output
Enter the number
7
Factorial of 7 is 5040
Analysis
f=1: When user gives 0 as input, then 0!=1The Loop: Loop is looped till n decreases to 1
f*=i: It is equal to f=f*i;. This is a simple factorial logic.
For people, who don't know what is factorial of a number, i could explain them with the help of examples for a better understand.
5!=5*4*3*2*1=120;
6!=6*5*4*3*2*1=6*(5!)=720;
Also see Finding Divisors of a number in Java
In this way, we can simply find factorial of a number without using any factorial function not only in Java but in any language.