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 r = 0;
        if (n < = 0) return 1;
          if (n > 3)
            {r = n;
            return f (n – 2) + 2;
            }
          return f(n – 1) + r;
    }
    What is the value of f(5)?
    1. 5
    2. 7
    3. 9
    4. 18
Correct Option: D

We follow, the following steps to obtain the value of f(5)
    f(5)
    r = 5

f(3) +2= 18
f(2) + 5= 16
f(1) + 5= 11
f(0) + 5= 6
1

So, f (5) = 1 + 5 + 5 + 5 + 2 = 18



Your comments will be displayed only after manual approval.