Write a program to display a right angle triangle of stars P

Write a program to display a right angle triangle of stars:

Prompt the user for the number of stars that they want

Display the triangle, where the first row has 1 star, second row has 2 stars,…

BONUS: Limit the input to the range of 0 to 20 Example 1: (Red is entered by user)

Number of stars: 1

*

Example 2:

Number of stars: 9

*

**

***

****

*****

******

*******

********

*********

Example 3: (Bonus) Number of stars: -1

Enter a valid number: 0

Enter a valid number: 21

Enter a valid number: 3

*

**

***

Please write code in Java

Solution

The java code is as follows:

class Star
{
public static void main(String[] args)
{
int i,j;
Scanner reader = new Scanner(System.in);
System.out.println(\"Enter a number: \");
int n = reader.nextInt();
if(n>0 && n<=20)
{
for(i=1; i<=n; i++)
{
for(j=1; j<i; j++)
{
System.out.print(\"*\");
}
System.out.println();
}
}
else
{
System.out.println(\"Enter a valid number\");
}
}
}

Write a program to display a right angle triangle of stars: Prompt the user for the number of stars that they want Display the triangle, where the first row has
Write a program to display a right angle triangle of stars: Prompt the user for the number of stars that they want Display the triangle, where the first row has

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site