The product of all natural numbers up to given natural number is called factorial . In maths, factorial is represented with the symbol ! . See the below examples.

    4! = 4*3*2*1 = 24
    5! = 5*4*3*2*1 = 120

Now, we are going to write a program to print factorial of given Integer

import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
       System.out.println("Please Enter Number: ");
       Scanner read = new Scanner(System.in);
       int a = read.nextInt();
       int result = 1;
       for (int i = 1; i <= a; i++) {
           result = result * i;
       }
       System.out.println("The factorial of "+a+" is : " + result);
   }
}

Leave a Reply

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