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)
View worked solution
Answer: 46:46
To find the output of the given C program, we can trace the execution of the functions step-by-step.
1. Analyze the Functions
- The function
g(n)is defined as:
- The function
f(n)callsg(n * 2), which is:
2. Simplify the Expression
Substituting into :
3. Trace the Loop in main()
The loop runs for and (since the condition is ):
- For :
sumbecomes . - For :
sumbecomes .
After , increments to , and the loop terminates. The program prints the value of sum, which is 46.
Free practice · Your answers on this page are not saved.
