0
Produce the following output using a method that applies recursion * ** *** **** *****
Interview question that disturbed my mind!
4 odpowiedzi
+ 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.
+ 1
@ wachirakorn , there is no recursion in your program
+ 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("*");
}
}
}
+ 1
function pyramid(count)
{
if (count<=0) return "";
var p=pyramid(count-1);
p=p + "*";
document.write(p+"<br\>");
return p;
}
pyramid (5);