Write the definition of a method named printPowerOfTwoStars that receives a non-negative integer n and prints a string consisting of “2 to the n” asterisks.

Write the definition of a method named printPowerOfTwoStars that receives a non-negative integer n and prints a string  consisting of “2 to the n” asterisks.

LANGUAGE: JAVA

CHALLENGE:

Write the definition of a method named printPowerOfTwoStars that receives a non-negative integer n and prints a string consisting of “2 to the n” asterisks. So, if the method received 4 it would print 2 to the 4 asterisks, that is, 16 asterisks:
****************
and if it received 0 it would print 2 to the 0 (i.e. 1) asterisks:
*
The method must not use a loop of any kind (for, while, do-while) to accomplish its job.

SOLUTION:

void printPowerOfTwoStars(int n) { 
	if (n == 0) {
		System.out.printf("*");
	}else{
		printPowerOfTwoStars (n-1); 
		printPowerOfTwoStars (n-1); 
	}
} 

Add comment