India's Largest Free Online Education Platform

Java program to calculate the factorial of a given number

Java program to calculate the factorial of a given number

Code:

import java.util.Scanner;
public class Factorial
{
    public static void main(String agrs[])
    {   System.out.println("Enter a number to find its factorial");
        Scanner Sc= new Scanner(System.in);
        int n= Sc.nextInt();

        int fact=1;
        for(int i=1;i<=n;i++)
            fact=fact*i;
        System.out.println("Factorial of "+n+" is "+fact);
    }
}

Output:

Leave a Reply

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

error: Content is protected !!