To find the factorial of a given number

0 minute read


To find the factorial of a given number.

Program:

import java.util.Scanner;

public class Factorial 

{

    public static void main(String[] args) 

    {

        int i,fact=1,n;

        System.out.println("Enter the number to find factorial:");

        Scanner sc= new Scanner(System.in);

        n=sc.nextInt();

        for(i=1;i<=n;i++)

            fact=fact*i;

        System.out.println("Factorial of a given number is: " +fact);

    } 

}

OutPut:

Enter the number to find factorial:

5

Factorial of a given number is: 120