Question Details

#include
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 ______.

Show Answer

Correct Answer :

46

Solution :

The correct answer is 46.

To find the output of the given C program, we can trace its execution step-by-step.

First, let's analyze the functions defined in the program:

1. Function g(n) takes an integer input n and returns n+10.

2. Function f(n) takes an integer input n and returns g(n*2). Substituting the definition of g, we have:

f(n)=(n*2)+10=2n+10

Inside the main function, the variable sum is initialized to 0. A for loop runs for n = 1 and n = 2 (as long as n < 3).

Iteration 1: n = 1

We calculate the value of f(1):

f(1)=g(1*2)=g(2)=2+10=12

Now, we compute g(f(1))=g(12):

g(12)=12+10=22

Updating the sum: sum = 0 + 22 = 22.

Iteration 2: n = 2

We calculate the value of f(2):

f(2)=g(2*2)=g(4)=4+10=14

Now, we compute g(f(2))=g(14):

g(14)=14+10=24

Updating the sum: sum = 22 + 24 = 46.

After the second iteration, the loop terminates. The final value printed is 46.

Unlock Our Free Library

Access expert-curated educational resources and study materials—completely free.

Ask AI Tutor
5 left
Q1 View Question & Options
AI Tutor is solving this question...
Reading question context & options...