Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. Consider the following C function :
        int f (int n)
        { static int i = 1;
        if (n > = 5) return n;
        n = n + 1;
         i ++;
        return f(n);
        }
    The value returned by f (1) is
    1. 5
    2. 6
    3. 7
    4. 8
Correct Option: C

The iterations that the given code will undergo are: From the given conditions we does not take into account when n = 1
Iteration 1
N = 1 + 1 = 2 therefore, i = 2
Iteration 2
N = 2 + 2 = 4 therefore, i = 3
Iteration 3
N = 4 + 3 = 2 = 4 therefore, i = 4
Hence, the value returned after three iterations is 7
When, i = 4 and it also fulfill the condition of n>=5



Your comments will be displayed only after manual approval.