Java Program:

- To print factorial of a number

Example:

User Input: 5

Output:
Factorial of 5 : 120

import java.util.Scanner;
public class Factorial {
  public static void main(String []args) {
    int num;
    long fact=1;
    Scanner userInput = new Scanner(System.in);
    System.out.print("Enter your number : ");
    num = userInput.nextInt();

    System.out.print("Factorial of "+num+" : ");
    for(int i=1; i<=num; i++)
      fact*=i;
    System.out.print(fact);
  }
}

Output:

  Enter your number : 3
  Factorial of 3 : 6
Program
number

You should know first :

Factorial of N : 1*2*3*4.....*N
Factorial of 0 : 1

The variable which holds factorial value (above fact) must be of type long

Because,
long : Factorial of a number can be very large [ valid for upto 20! ]

for(int i=1; i<=num; i++)
  fact*=i;

For greater than 20! , you have to use BigInteger of Math library

Must import java.math.BigInteger in this case

for(int i=1; i<=num; i++)
  fact = fact.multiply(BigInteger.valueOf(i));
Concept

Coming Soon !

Quick
Tutorial

Quote

The best preparation for good work tomorrow is to do good work today.