Advertisements
Advertisements
प्रश्न
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.
दीर्घउत्तर
Advertisements
उत्तर
int p, n = 5, f = 1;
- The program segment has a logical error.
- Type of Error: Logical Error.
- The variable
fis initialised to 0. Since any number multiplied by zero is zero, the calculationf *= pwill always result in0. To calculate a factorial, the initial value of the product variable must be 1. - To get the desired output, change the initialisation of
ffrom0to1.
shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
2025-2026 (March) Official Board Paper
