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.

Computer Science & Information Technology

You might also like to view...

Analyze the following code:

``` public class Test1 { public static void main(String[] args) { xMethod(new double[]{3, 3}); xMethod(new double[5]); xMethod(new double[3]{1, 2, 3}); } public static void xMethod(double[] a) { System.out.println(a.length); } }``` a. The program has a compile error because xMethod(new double[]{3, 3}) is incorrect. b. The program has a compile error because xMethod(new double[5]) is incorrect. c. The program has a compile error because xMethod(new double[3]{1, 2, 3}) is incorrect. d. The program has a runtime error because a is null.

Computer Science & Information Technology

A computer that requests the services of a server is best known as a:

A) user B) customer C) patron D) client

Computer Science & Information Technology

Match the following features or parts of Office 2013 to their descriptions:

I. contextual tab II. Mini toolbar III. title bar IV. gallery V. status bar A. easy access to formatting commands B. contains groups of commands related to a specific, selected object C. set of selections when you click a More button D. contains information relative to the open file E. includes file name and application

Computer Science & Information Technology

During a(n) _______________ installation, the new operating system can be installed into a different subdirectory on a different drive, leaving the old operating system intact and usable.

A. automated B. clean C. multiboot D. upgrade

Computer Science & Information Technology