Skip to content
Related Articles

Related Articles

Algorithms | Recursion | Question 6
  • Difficulty Level : Medium
  • Last Updated : 03 Jul, 2020
GeeksforGeeks - Summer Carnival Banner

Output of following program?




#include<stdio.h>
void print(int n)
{
    if (n > 4000)
        return;
    printf("%d ", n);
    print(2*n);
    printf("%d ", n);
}
  
int main()
{
    print(1000);
    getchar();
    return 0;
}

(A) 1000 2000 4000
(B) 1000 2000 4000 4000 2000 1000
(C) 1000 2000 4000 2000 1000
(D) 1000 2000 2000 1000


Answer: (B)

Explanation: First time n=1000
Then 1000 is printed by first printf function then call print(2*1000) then again print 2000 by printf function then call print(2*2000) and it prints 40000 next time print(4000*2) is called.

Here 8000 is greater than 4000 condition becomes true and it return at function(2*4000). Here n=4000 then 4000 will again print through second printf.

Similarly print(2*2000) after that n=2000 then 2000 will print and come back at print(2*1000) here n=1000, so print 1000 through second printf.

Option (B) is correct.

Quiz of this Question

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :