Factorial Program in Java - Java Program for Factorial of a Number



Factorial Program in Java :

  • Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example: 3! = 3*2*1 = 6

Sample Code

import java.util.Scanner;

public class factorial 
{
  public static void main(String[] args) 
  {
    int fact = 1;
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the factorial of ___ ?");
    int number = sc.nextInt();
    for (int i = 1; i <= number; i++) 
    {
      fact = fact * i;
    }
    System.out.println("Factorial of " + number + " is: " + fact);
  }
}

Output

Enter the factorial of ___ ? 5
Factorial of 5 is: 120
Enter the factorial of ___ ? 10
Factorial of 10 is: 3628800

Related Searches to Factorial Program in Java - Java Program for Factorial of a Number