GATE 2025 CS (CS2) – Question 63
Consider the following C program:
#include <stdio.h>
int g(int n) {
return (n+10);
}
int f(int n) {
return g(n*2);
}
int main() {
int sum, n;
sum=0;
for (n=1; n<3; n++)
sum += g(f(n));
printf ("%d", sum);
return 0;
}The output of the given C program is ________. (Answer in integer)
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: 46
Explanation
`g(n)` returns $n+10$ and `f(n)` returns `g(n*2)` $=2n+10$. So `g(f(n))` $=f(n)+10=2n+20$.
The loop `for (n=1; n<3; n++)` runs for $n=1$ and $n=2$:
- $n=1$: $2(1)+20=22$
- $n=2$: $2(2)+20=24$
$\text{sum}=22+24=\mathbf{46}$.