Docsity

Java Exercises: Printing a Triangle with Nested Loops, Study notes of Software Engineering

Partial preview of the text

Download Java Exercises: Printing a Triangle with Nested Loops and more Study notes Software Engineering in PDF only on Docsity!

Exercises โ€“ Nested Loops

Java โ€“ An Introduction to Problem

Solving & Programming, Walter

Savitch

Page 219, Problem 8

โ€ข^

Write a program that asks the user to enter the size of atriangle to print out (an integer from 1 to 50), then printthe triangle by printing a series of lines consisting ofasterisks. The first line will have one asterisk, the nexttwo, and so on, with each line having one more asteriskthan the previous line, up to the number entered by theuser. On the next line print one less asterisk andcontinue by decreasing the number of asterisks by 1 foreach successive line until only one asterisk is printed. Hint

: Use nested

for

loops; the outside loop controls the

number of lines to print, and the inside loop controls thenumber of asterisks to print on a line. For example, if theuser enters

, the output would be (next page)

A variation of the problem

-^

Write a Java program with the followingspecifications.โ€“ Input: An integer

height

  • Output: A triangle. (See example below).โ€“ Example: If the input

height

is

, then print the

following.