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

A problem from the book 'java - an introduction to problem solving & programming' by walter savitch. The problem asks to write a java program that prints a triangle of asterisks based on user input. The triangle should have a number of lines equal to the user input, with each line having one more asterisk than the previous line. A variation of the problem asks to print a triangle with a given height, where the outside loop controls the number of lines and the inside loop controls the number of asterisks on each line. For example, if the height is 5, the output would be: * * * * * * * * * * * * * * * * * * * * * * * * *

Typology: Study notes

Pre 2010

Uploaded on 08/01/2009

koofers-user-yog-2
koofers-user-yog-2 🇺🇸

8 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Exercises – Nested Loops
Java – An Introduction to Problem
Solving & Programming, Walter
Savitch
pf3
pf4

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.