Programmers puzzle

749.0K Views
Answered

What will be the output of the program-
print(5);

void print (int n){

       if (n>0){

               printf(“hello”);

               print(n-1);

       }

       printf(“world”);

}

Share
Add Comment

  • 3 Answer(s)
    Best answer


    For print(5)
    Output-
    hellohellohellohellohelloworldworldworldworldworldworld

    Suppose n=3
    The output shall be
    hellohellohelloworldworldworldworld

    Basically, the number of hello’s shall be equal to value of n and the number of world’s following it shall be n+1.

    psn Guru Answered on 19th May 2016.
    Add Comment

    print(5) will print hellohellohellohellohelloworldworldworldworldworldwolrd.
    basically print(n) will print hello…..(n times)world(n+1 times)

    dyj Expert Answered on 19th May 2016.
    Add Comment

    Hi,

    The code prints Hello for N times as if(n>0) is TRUE

    and it prints World also for N times as if(n>0) is TRUE  1 more World for if(n>0)  FALSE case.

    Horteppa Starter Answered on 6th June 2016.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.
  • More puzzles to try-