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

int factorial = 1;

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

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 : A

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 required values 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 assigned to factorial.
Iteration 1 : factorial = 1 * 1
Iteration 2 : factorial = 1 * 1
Similarly all the other 4 iterations are executed.
After the termination of for loop, value of factorial is printed as 1.