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

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);
}

The correct answer is
2550

Analyzing the Recursive Function f(n)

The function $f(n)$ is defined with two cases:

  • Base Case: If $n$ equals 0, the function returns 0.
  • Recursive Step: Otherwise (if $n$ is not 0), the function returns the sum of $n$ and the result of calling itself with $n-2$.

Evaluating f(n) for n = 100

We need to calculate the value of $f(100)$. Let's trace the execution:

$f(100) = 100 + f(98)$
$f(98) = 98 + f(96)$
$f(96) = 96 + f(94)$
$...$
$f(4) = 4 + f(2)$
$f(2) = 2 + f(0)$
$f(0) = 0$

By substituting the results back up the chain, we see that:

$f(100) = 100 + 98 + 96 + ... + 4 + 2 + 0$

Calculating the Sum of the Series

The value $f(100)$ represents the sum of even numbers from 2 up to 100.

The series is $ S = 2 + 4 + 6 + \dots + 98 + 100 $.

This is an arithmetic progression.

  • First term ($a$) = 2
  • Last term ($l$) = 100
  • Common difference ($d$) = 2

First, find the number of terms ($k$). Using the formula $ l = a + (k-1)d $: $ 100 = 2 + (k-1)2 $ $ 98 = (k-1)2 $ $ 49 = k-1 $ $ k = 50 $ So, there are 50 terms in the series.

Next, calculate the sum ($S_k$) using the formula $ S_k = \frac{k}{2}(a + l) $: $ S_{50} = \frac{50}{2}(2 + 100) $ $ S_{50} = 25 \times 102 $ $ S_{50} = 2550 $

Thus, the function $f$ returns 2550 when $n = 100$.

Was this answer helpful?

Important Questions from Function Recursion

  1. The process of removing recursion involves replacing recursive function calls with:

  2. #include <stdio.h>
    
    int foo(int S[], int size){
        if(size == 0) return 0;
        if(size == 1) return 1;
        if(S[0] != S[1]) return 1 + foo(S + 1, size - 1);
        return foo(S + 1, size - 1);
    }
    
    int main(){
        int A[] = {0, 1, 2, 2, 2, 0, 0, 1, 1};
        printf("%d", foo(A, 9));
        return 0;
    }
    

    The value printed by the given C program is _______ . (Answer in integer)

  3. Consider the recursive functions represented by the following code segment:
    int bar(int n){
    if (n == 1) return 0;
    else return 1 + bar(n/2);
    }
    int foo(int n){
    if (n == 1) return 1;
    else return 1 + foo(bar(n));
    }

    The smallest positive integer n for which foo(n) returns 5 is ______. (answer in integer)
    Note: Ignore syntax errors (if any) in the function.
  4. Consider the following ANSI-C function.

    int func(int start, int end){
    int length=end+1-start;
    if((length < 1)||(start < 0)||(end < 0)){ return(0); }
    if(length%3==0){
    return(func(start+1, end));
    } else if(length%3==1){
    return(1+func(start, end-1));
    } else {
    return(func(start+2, end));
    }
    }


    The maximum possible value that can be returned from this function is ____________. (answer in integer)

    Note: Ignore syntax errors (if any) in the function.
  5. What is the output of the given C language code snippet? 

    #include<stdio.h>

     void f(int n)

    {

     if(n==0)

     return; 

    printf("%d ",n); 

    f(n-1); 

    int main()

    f(3); 

    return 0; 

    }

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