July2016 cs Q15

0. What is the value returned by the function f given below when n = 100? int f (int n) { if (n = = 0) then return n; else return n + f(n-2); }

  • Option : A
  • Explanation :
    We have function:
    int f (int n)
    { if (n = = 0) then return n;
    else
    return n + f(n-2);
    }
    we have to find output for n = 100 i.e int f (100) { if (n = = 0) then return n; //failure// else return 100 + f (98) { if (n = = 0) then return n; //failure// else return 98 + f (96) { if (n = = 0) then return n; //failure// else return 96 + f (94).......................f(0);// It will be a AP series of 100 , 98, 96,.........0// ; }; }; } Sum of series when we know first and last term and no of terms: Sn = n / 2 * (first term + last term) i.e. 51 / 2 * (100 + 0). = 51 * 50 = 2550. So, option (A) is correct.
Cancel reply

Your email address will not be published. Required fields are marked *


Cancel reply

Your email address will not be published. Required fields are marked *