The following program segment calculates and displays the factorial of a number. [Example : Factorial of 5 is 1 x 2 x 3 x 4 x 5 = 120]

ICSE Class 10 Computer Applications PYQ · 2026 Paper · Section A

2(vii)1 Mark · 2026 · Section A · 2026 Paper

The following program segment calculates and displays the factorial of a number. [Example : Factorial of 5 is 1 x 2 x 3 x 4 x 5 = 120]

int p, n = 5, f = 0;
for(p = n; p > 0; p--)
f *= p;
System.out.println(f);

Name the type of error if any; correct the statement to get the desired output.

Show SolutionHide Solution↓

The logical error occurs because the factorial variable f is initialized to 0. Since multiplication with 0 always results in 0, the loop will never produce the correct factorial value.

The corrected code is as follows:

int p, n = 5, f = 1;
for(p = n; p > 0; p--)
f *= p;
System.out.println(f);
← Previous questionNext question →

Browse the free Computer Applications sample bank