Consider the following C program: #include <stdio.h> int g(int n) { return (n…

2025

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)

Attempted by 59 students.

Show answer & explanation

Correct answer: 46

Short answer: 46

Explanation:

  1. g(n) returns n + 10.

  2. f(n) calls g with n * 2, so f(n) = g(n * 2) = (n * 2) + 10 = 2n + 10.

  3. The expression g(f(n)) equals f(n) + 10 = (2n + 10) + 10 = 2n + 20.

  4. The loop runs for n = 1 and n = 2 (since n < 3). For n = 1, g(f(1)) = 2*1 + 20 = 22. For n = 2, g(f(2)) = 2*2 + 20 = 24.

  5. Sum = 22 + 24 = 46, which is printed by printf.

Output: 46

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir