212k views
0 votes
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:

User Belek
by
8.4k points

1 Answer

2 votes

Answer:

import java.io.*;

import java.util.*;

import java.lang.Math;

class GFG {

public static void PowerOfTwoStars(int n){

double a=Math.pow(2,n); //calculating the power..

for (int i=1;i<=a;i++)//looping to print the stars.

{

System.out.print("*");

}

}

public static void main (String[] args) {

Scanner star=new Scanner(System.in);//creating the scanner class object.

int n=star.nextInt();//taking input.

PowerOfTwoStars(n);//calling the function.

}

}

Input:-

3

Output:-

********

Step-by-step explanation:

In the method PowerOfTwoStars which contains one argument I have calculated the power that is 2 to the n.Then looping form 1 to power and printing the stars and for better explanation please refer the comments the code.

User Thehme
by
7.9k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.