Open In App
Related Articles

Algorithms | Recursion | Question 6

Improve Article
Save Article
Like Article

Output of following program? 


void print(int n)
    if (n > 4000)
    printf("%d ", n);
    printf("%d ", n);
int main()
    return 0;


1000 2000 4000


1000 2000 4000 4000 2000 1000


1000 2000 4000 2000 1000


1000 2000 2000 1000

Answer: (B)


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 4000 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 print. 

Hence Option (B) is correct.

Quiz of this Question
Please comment below if you find anything wrong in the above post

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 01 Jun, 2021
Like Article
Save Article
Similar Reads