All Exams Test series for 1 year @ ₹349 only
Question

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)

To determine the output of the provided C program, let's analyze the code:

  • The program includes two functions: g(int n) and f(int n).
    • g(int n) returns (n+10).
    • f(int n) calls g(n*2), meaning f(n) will return g(n*2) = n*2+10.
  • Inside the main function:
    • Variables sum and n are declared, with sum initialized to 0.
    • A for loop runs with n starting at 1 and ending before 3.

Let's examine the loop:

Iterationnf(n)g(f(n))
11f(1) = g(1*2) = g(2) = 2+10 = 12g(12) = 12+10 = 22
22f(2) = g(2*2) = g(4) = 4+10 = 14g(14) = 14+10 = 24

Calculating the sum:

  • Iteration 1: sum = 0 + 22 = 22
  • Iteration 2: sum = 22 + 24 = 46

The final value of sum is 46.

Verification:
The output, 46, matches the specified range (46, 46). Therefore, the computed value is correct.

Therefore, the output of the program is 46.

Was this answer helpful?

Important Questions from Programming in C

  1. Determine what's true for x, if we define int *x[10] ?
  2. In bitwise operator, each right shift operator divides by ________.
  3. In Switch-case, the ________ is not allowed to be case labels.
  4. In C++, ________ functions are not actually called, rather, their codes are expanded
  5. What is the output of the following code ? 

    my recursive<-_function(n) 

    }{ 

    if(n = O) return

     print(n) 

    my_recursive(n-1); 

    }{ my_recursive_function(10)

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App