Programmers puzzle
1,135.1K 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”); }
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.
print(5) will print hellohellohellohellohelloworldworldworldworldworldwolrd.
basically print(n) will print hello…..(n times)world(n+1 times)
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.
Your Answer
More puzzles to try-
Crossing a River Riddle
John and Jacob want to cross a river. The only way to get to the other side of the river ...Read More »Which is the odd one out
Find The Odd One Out 1. FLOW 2. SNIP 3. TRAP 4. DRAW 5. BACKRead More »Cabins Puzzle
Far off in the woods, there is a cabin. The cabin contains 3 people. 2 are in one room together, ...Read More »What am I???
I give refuge to the blind and repeat everything they say. What am I??Read More »3 accessories with 100 rupees
A man buy 3 accessories with 100 rupees. first one 5 rupees, second one 3 rupees, third one 0.5 rupees ...Read More »What is 4 and 5?
Two is company and 3 is a crowd. What is 4 and 5?Read More »The Leaked Room
A man is dead in a puddle of blood and water on the floor of a locked windowless room. What ...Read More »Fun Laughter Challenge
We know that money can be names differently for the purpose it is used for. Some of the examples of ...Read More »25 horses 5 tracks Puzzle
Mr John have 25 horses, and he want to pick the fastest 3 horses out of those 25. He has only ...Read More »Who am I?
I am a 8 letter word Many wasted time in hunting me, rather I’m in the form of hardwork. 5,6,7,8 ...Read More »What happened to Angel and Dory
Angel and Dory were found on the ground gasping for air surrounded by glass and water. The person who found ...Read More »New Orleans under Water
New Orelans was founded in 1718, over 290 years ago. Hurricane Katrina brought flooding and devastation to New Orleans in ...Read More »What is next
If, 4+2=26, 8+1=79 and 6+5=111. Then, what is 7+3=……Read More »Man with appetite riddle
Which country would you send a man to for his appetite?Read More »Missing Digit Riddle
The centre of the third figure is empty. What digit should fit inside?Read More »Crossing the Bridge Puzzle
Four people need to cross a rickety bridge at night. Unfortunately, they have only one torch and the bridge is ...Read More »Odd even riddle
If, Even numbers are Red and odd numbers are Black… …what colour is Black + Black?Read More »Color of the bean
A pot contains 75 white beans and 150 black ones. Next to the pot is a large pile of black ...Read More »How does he know what is inside the box?
A man walks into a completely dark room. There is a box on a table at the other end of ...Read More »Favorite drink
He goes out drinking every night, returning to his home in the wee hours of every morning. No matter how ...Read More »
Accenture Interview PuzzlesAdobe Interview PuzzlesAge RiddleAkbar Birbal PuzzlesAlgorithm PuzzlesAlphabet riddleAmazon Interview PuzzlesAnalytical MathematicsAptitude PuzzleBank PuzzlesBetting PuzzlesBrain TeasersCalendar PuzzlesCards PuzzlesChess Board PuzzleChess PuzzlesChristmas Puzzlecipher PuzzleCivil Services PuzzleClock time puzzleCognizant Interview PuzzlesCoins PuzzleComputational PuzzleconundrumCoronavirus PuzzleCoupondunia Interview PuzzleCritical Thinking Puzzledata analyticsData Structure Interview QuestionsDecode PuzzleDetective PuzzlesDice PuzzleDictionary Riddlesdifficult riddleEasy Math puzzlesEasy puzzlesEinstein puzzleEnglish RiddleFamily Tree Puzzlefamous puzzleFill in the blanks riddlesFinding Killer RiddlesFlipkart interview puzzlesFunny RiddlesGeneral KnowledgeGeographical PuzzleGoogle Code Jam 2014Google Interview PuzzlesGRE PuzzleHard Puzzle