Uncategorized

Java program to find factorial of a number.

Java program to find factorial of a number.

import java.util.Scanner;
public class Factorial 
{
    public static void main(String args[]) {
        int n, f = 1;
        System.out.print("Enter any number:");
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) 
        {
            f = f * i;
        }
        System.out.println("Factorial of " + n + " is=" + f);
    }
}

Output

Enter any number:5
Factorial of 5 is=120

Leave a Reply

Your email address will not be published. Required fields are marked *