16 votes 16 votes Consider the following function definition. void greet(int n) { if(n>0) { printf("hello"); greet(n-1); } printf("world"); } If you run $\textsf{greet(n)}$ for some non-negative integer $\textsf{n},$ what would it print? $\textsf{n}$ times "hello", followed by $\textsf{n+1}$ times "world" $\textsf{n}$ times "hello", followed by $\textsf{n}$ times "world" $\textsf{n}$ times "helloworld" $\textsf{n+1}$ times "helloworld" $\textsf{n}$ times "helloworld", followed by "world" Programming in C tifr2018 programming programming-in-c recursion + – Arjun asked Dec 10, 2017 edited Nov 20, 2022 by Lakshman Bhaiya Arjun 2.8k views answer comment Share Follow See all 9 Comments See all 9 9 Comments reply Show 6 previous comments gauravkc commented Jan 24, 2018 reply Follow Share When n=0, printf Hello will not execute. But printf world will 1 votes 1 votes Devshree Dubey commented Jan 24, 2018 reply Follow Share Okay. Thank you so much!!! 0 votes 0 votes Rohit Gupta 8 commented Jan 24, 2018 reply Follow Share Here it is. https://gateoverflow.in/179276/tifr2018-a-7 0 votes 0 votes Please log in or register to add a comment.
Best answer 8 votes 8 votes Take n = 2: void greet(2) { if(2>0) { printf("hello"); //print number 1 greet(1); } printf("world"); //3rd time print } void greet(1) { if(1>0) { printf("hello"); //print number 2 greet(0); } printf("world"); //2nd time print } void greet(0) { if(0>0) fail {} printf("world"); //1st time print } Output = hellohelloworldworldworld i.e. $n$ times hello and $n+1$ times world. Correct Answer: $A$ Anu007 answered Dec 10, 2017 edited May 8, 2019 by Naveen Kumar 3 Anu007 comment Share Follow See all 0 reply Please log in or register to add a comment.
2 votes 2 votes By close examine we can see that if portion will run for n times where as else will run for n+1 times "+1" when value of integer n=0 so n times hello and n+1 times world Tesla! answered Dec 10, 2017 Tesla! comment Share Follow See all 0 reply Please log in or register to add a comment.
2 votes 2 votes Let's take n=5 and dry run the given recursive function. Tree will look like this and one can simply count the numbers. supreetshukla answered Apr 11, 2021 supreetshukla comment Share Follow See all 0 reply Please log in or register to add a comment.
1 votes 1 votes option A n times hello, when n=0 it returns from function and prints "world" 1 time followed by n times "world" Manoja Rajalakshmi A answered Dec 10, 2017 Manoja Rajalakshmi A comment Share Follow See all 0 reply Please log in or register to add a comment.