edited by
2,801 views
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?

  1. $\textsf{n}$ times "hello", followed by $\textsf{n+1}$ times "world"
  2. $\textsf{n}$ times "hello", followed by $\textsf{n}$ times "world"
  3. $\textsf{n}$ times "helloworld"
  4. $\textsf{n+1}$ times "helloworld"
  5. $\textsf{n}$ times "helloworld", followed by "world"
edited by

5 Answers

0 votes
0 votes
Jusk take n as 0

World is getting printed only

 

now take n=1

Hello World followed by world

 

by Iteration,

you can see when you take n=k

k times (hello world) followed by one single world which was the base condition

 

Hence

Option A
Answer:

Related questions

23 votes
23 votes
5 answers
1
13 votes
13 votes
1 answer
2
30 votes
30 votes
6 answers
3