0

Produce the following output using a method that applies recursion * ** *** **** *****

Interview question that disturbed my mind!

29th Jul 2016, 5:17 AM
Nick Nderitu
Nick Nderitu - avatar
4 Réponses
+ 1
I can't currently write the code but it'd have a base case of one to exit the method. It would take an integer parameter for the maximum amount of stars. Then it'd print the first star and call it's self with the number minus 1. The next time though it'd draw two and subtract another. The parameter would seem to be the opposite of the stars for the output they want because it would decrease while the stars increase, but the nmain point is that the base case is 1. Hopefully this helps you write the code.
29th Jul 2016, 6:41 AM
nk361
nk361 - avatar
+ 1
@ wachirakorn , there is no recursion in your program
29th Jul 2016, 6:46 AM
Nick Nderitu
Nick Nderitu - avatar
+ 1
Sorry, misunderstood. Here is the new one. public class Program { public static void main(String[] args) { printStar(5); } static void printStar(int n){ if(n>1){ printStar(n-1); System.out.println(); } for(int i=1; i<=n; i++) { System.out.print("*"); } } }
29th Jul 2016, 7:14 AM
WPimpong
WPimpong - avatar
+ 1
function pyramid(count) { if (count<=0) return ""; var p=pyramid(count-1); p=p + "*"; document.write(p+"<br\>"); return p; } pyramid (5);
2nd Aug 2016, 6:44 PM
Sumitabha Banerjee
Sumitabha Banerjee - avatar