SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Primitive Data And Definite Loops
Exercise:
Exercises
Question:17 | ISBN:9780136091813 | Edition: 2

Question

Modify your program from the previous exercise so that it uses a global constant for the figure’s height. (You may want to make loop tables first.) The previous output used a constant height of 6. The following are the outputs for constant heights of 4 and 8:

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

public class Ch02Ex17
{
	public static final int HEIGHT = 8;

	public static void main(String[] args)
	{
		for (int r = 1; r <= HEIGHT; r++)
		{
			for (int c = 1; c <= 2 * r - 2; c++)
			{
				System.out.print("\\");
			}

			for (int c = 1; c <= 4 * HEIGHT - 4 * r + 2; c++)
			{
				System.out.print("!");
			}

			for (int c = 1; c <= 2 * r - 2; c++)
			{
				System.out.print("/");
			}

			System.out.println();
		}
	}
}

Output:

0 0

Discussions

Post the discussion to improve the above solution.