The value of $j$ at the end of the execution of the following C program:
int incr (int i) { static int count = 0; count = count + i; return (count); } main () { int i, j; for (i = 0; i <= 4; i++) j = incr (i); }
is:
Answer: (A)
In main function inside the for loop incr(i) function call is done for each value of i from 0 to 4 and in the incr(function definition) just adding all of them (because of count variable is static and it retains previously result).
So, if i goes from 0 to 4(inside the main) then incr(function definition) last value returned will be –
0+1+2+3+4 = $\frac{4*5}{2}$ = 10
Similarly, for i goes from 0 to n incr last value returned will be –
0+1+2+….n = $\frac{n*(n+1)}{2}$
64.3k questions
77.9k answers
244k comments
80.0k users