Write a program that asks the user to enter the size of a triangle (an integer from 1 to 50). Display the triangle by writing lines of asterisks. The first line will have one asterisk, the next two, and so on, with each line having one more asterisk than the previous line, up to the number entered by the user. On the next line write one fewer asterisk and continue by decreasing the number of asterisks by 1 for each successive line until only one asterisk is displayed. Hint: Use nested for loops; the outside loop controls the number of lines to write, and the inside loop controls the number of asterisks to display on a line. For example, if the user enters 3, the output would be
*
**
***
**
*
This project includes input checking so it will not print any lines with asterisks if the user enters a number less than 1 or greater than 50. If a valid number is entered, two pairs of nested for-loops are used to print the triangle of asterisks. The first nested pair prints the lines with an increasing number of asterisks, starting with one and increasing by one per line up to a maximum of the number entered by the user. The second nested pair of for-loops prints the lines with a decreasing number of asterisks, starting with (number –1) down to 1. The outside loops count through the lines printed and the inside loops count through the number of asterisks printed on the line. The number of asterisks to print on any line is its line number set by the outside loop.
See the code in TriangleOfAsterisks.java.
You might also like to view...
Software that is distributed free along with the program source code is called:
A) closed source software. B) open source software. C) Microsoft Windows. D) outsource software.
In Word or PowerPoint, a quick way to select an entire paragraph is to:
What will be an ideal response?