The factorial function (symbol: !) means to multiply a series of descending natural numbers.
Example: 4! is shorthand for 4 x 3 x 2 x 1.
import java.util.*;
class factorial
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,f=1;
System.out.println("Enter the no:");
n=sc.nextInt();
for(int i=1;i<=n;i++)
{
f=f*i;
}
System.out.println("The factorial is"+f);
}
}
Output:
Enter the no:
5
The factorial is 120
Example: 4! is shorthand for 4 x 3 x 2 x 1.
- 4! = 4 × 3 × 2 × 1 = 24
- 5! = 5 × 4 × 3 × 2 × 1 =120
- 1! = 1
class factorial
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n,f=1;
System.out.println("Enter the no:");
n=sc.nextInt();
for(int i=1;i<=n;i++)
{
f=f*i;
}
System.out.println("The factorial is"+f);
}
}
Output:
Enter the no:
5
The factorial is 120