Program:

import java.util.Scanner;
public class Factorial
{
 public static void main(String[] args)
 {
 int x=1;
 Scanner sc = new Scanner(System.in);
 System.out.print("Enter a number to find the factorial: ");
 int n = sc.nextInt();
 
 for(int i=n; i>0; i--)
 {
 x = x*i;
 }
 System.out.println("Factorial of "+n+" is "+x);
 }
}

Output:

Enter a number to find the factorial: 4
Factorial of 4 is 24

You can download the pdf file of Factorial.

Advertisements