Java Program to Find Factorial of a Number

In this example, we are giving a program to find a factorial of a number.


To find a factorial of a number:

  1. # To find a factorial of a number
  2. public classFactorial {
  3. public static void main(String[] args) {
  4. int num = 10;
  5. long factorial = 1;
  6. for(int i = 1; i <= num; ++i) {
  7. factorial *= i;
  8. }
  9. System.out.printf("Factorial of %d = %d", num, factorial);
  10. }
  11. }

Output:

Factorial of 7 = 5040