Calculate Factorial in Java

In this tutorial, we will calculate factorial of an user input (integer) and to do this, we will create a for loop for multiplying numbers

Calculate Factorial in Java

Let's use a for loop and calculate the factorial of user input.

Scanner input = new Scanner(System.in);
System.out.println("Type a number to calculate factorial");
int number = input.nextInt();
int factorial = 1;

for (int i=2; i<=number; i++)
	factorial*=i;
		  
System.out.println(number+"! = "+factorial);

Output:

Type a number to calculate factorial
5
5! = 120