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++)
System.out.println(number+"! = "+factorial);
Type a number to calculate factorial
5! = 120
© Copyright Vitademy 2022. All rights reserved.