Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to display a right angle triangle of stars: Prompt the user for

ID: 3779504 • Letter: W

Question

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

Explanation / Answer

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");
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote