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:15 | ISBN:9780136091813 | Edition: 2

Question

Write a method called printDesign that produces the following output. Use nested for loops to capture the structure of the figure.
-----1-----
----333----
---55555---
--7777777--
-999999999-

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program:

public class Ch02Ex15
{
	public static void main(String[] args)
	{
		printDesign();
	}

	public static void printDesign()
	{
		int n = 1;
		for (int i = 1; i <= 5; i++)
		{
			for (int j = 1; j <= 6 - i; j++)
			{
				System.out.print("-");
			}

			for (int j = 1; j <= n; j++)
			{
				System.out.print(n);
			}

			for (int j = 1; j <= 6 - i; j++)
			{
				System.out.print("-");
			}

			System.out.println();
			n += 2;
		}
	}
}

Output:

-----1-----
----333----
---55555---
--7777777--
-999999999-

 

0 0

Discussions

Post the discussion to improve the above solution.