Skip to content
Official PYQ · 2025

GATE CS 2025 Set 2 Q53 — Programming in C

Explain with AI

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:
    g(n)=n+10g(n) = n + 10
  • The function f(n) calls g(n * 2), which is:
    f(n)=g(2n)=2n+10f(n) = g(2n) = 2n + 10

2. Simplify the Expression g(f(n))g(f(n))

Substituting f(n)f(n) into g(x)g(x):

g(f(n))=g(2n+10)=(2n+10)+10=2n+20g(f(n)) = g(2n + 10) = (2n + 10) + 10 = 2n + 20

3. Trace the Loop in main()

The loop runs for n=1n = 1 and n=2n = 2 (since the condition is n<3n < 3):

  • For n=1n = 1: g(f(1))=2(1)+20=22g(f(1)) = 2(1) + 20 = 22 sum becomes 0+22=220 + 22 = 22.
  • For n=2n = 2: g(f(2))=2(2)+20=24g(f(2)) = 2(2) + 20 = 24 sum becomes 22+24=4622 + 24 = 46.

After n=2n = 2, nn increments to 33, and the loop terminates. The program prints the value of sum, which is 46.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions