class Factorial { public static void main(String s[]) { int number = 5;

int factorial = 1;

for(int i = 2; i <= number; i++ ) { factorial *= number; }

System.out.println("Factorial of 5 is " + factorial); } }

Factorial of 5 is 1

Factorial of 5 is 120

Compilation Errors

Factorial of 5 is 625

Correct Answer : D

An integer variable number is assigned to 5 whose factorial is to be calculated. Another integer variable factorial is declared to store factorial of number and is initialized to 1.
NOTE: It is good practice to initialize variables to avoid bugs.
In the for loop:
Initializing condition : i = 2
Terminating Condition : i <= number
Increment value : 1 (i++)
Number of iterations : number -1
In every iteration factorial is multiplied with factorial and assingned to factorial.
Iteration 1 : factorial = 1 * 5
Iteration 2 : factorial = 5 * 5
Iteration 3 : factorial = 25 * 5
Iteration 4 : factorial = 125 * 5 = 625

After the termination of for loop value of factorial is printed as 625.