Skip to content
Related Articles

Related Articles

GATE | GATE-CS-2015 (Set 3) | Question 49
  • Last Updated : 24 Feb, 2015

Consider the following recursive C function. If get(6) function is being called in main() then how many times will the get() function be invoked before returning to the main()?




void get (int n)
{
   if (n < 1) return;
   get(n-1);
   get(n-3);
   printf("%d", n);
}


(A) 15
(B) 25
(C) 35
(D) 45


Answer: (B)

Explanation:

                              get(6) [25 Calls]
                              /      \
               [17 Calls] get(5)       get(3) [7 Calls]
                        /     \
                    get(4)    get(2)[5 Calls]
                   /    \ 
     [7 Calls] get(3)  get(1)[3 Calls]
                /     \
             get(2)   get(0)
            /    \
[3 Calls]get(1)  get(-1) 
   /  \
get(0) get(-2)

We can verify the same by running below program.




# include <stdio.h>
int count = 0;
  
void get (int n)
{
    count++;
    if (n < 1) return;
    get(n-1);
    get(n-3);
}
int main()
{
    get(6);
    printf("%d ", count);
}


Output: 25

Quiz of this Question

sudo-gate-course

My Personal Notes arrow_drop_up
Recommended Articles
Page :