Write a program using a Scanner that asks the user for a number n between 1...

70.2K

Verified Solution

Question

Programming

Write a program using a Scanner that asks the user for a numbern between 1 and 9 (inclusive). The program prints atriangle with 2n - 1 rows. The first row contains only thesquare of 1, and it is right-justified. The second row contains thesquare of 2 followed by the square of 1, and is right justified.Subsequent rows include the squares of 3, 2, and 1, and then 4, 3,2 and 1, and so forth until n rows are printed. Startingat row n + 1, the squares between (n- 1) to 1 are printed, again right justified.  Rown + 2 prints the squares between (n -2) to 1.

Assuming the user enters 4, the program prints the followingtriangle of 7 rows to the console

       1     4 1   9 4 116 9 4 1   9 4 1     4 1       1

If the user enters 7, the following 13-row triangle isprinted

               1             4 1           9 4 1         16 9 4 1      25 16 9 4 1   36 25 16 9 4 1 49 36 25 16 9 4 1   36 25 16 9 4 1      25 16 9 4 1        16 9 4 1            9 4 1             4 1               1
 

Notes

  • Your solution should NOT use theconditionaloperator
  • Your solution should NOT use tabs
  • Your solutions SHOULD have at least one nested loop

Hint

Don't think of the output as a triangle. Think of it as tworectangular tables: one of the first n rows, the second ofthe last (n-1) rows.

Within each table, some cells are three spaces, some are onespace and two digits, and some are two spaces and one digit.

Start by printing an entire table with each cell its appropriatesquare value. Then figure out how to replace the cells that should\"empty\" with three spaces instead of a number.

Finally, figure out how to print the one-digit numbers as twospaces and one digit, and the two-digit numbers as one space andtwo digits.

Grading Elements

  • Program asks the user for a number in the specified range
  • Program displays the correct number of rows
  • Program displays the correct number of columns
  • Each column entry contains the appropriate square number
  • Each row is right-justified, with 1 in the right-mostcolumn
  • Row spacing is consistent, with each column's entry directlybelow the preceding row
  • Program does not use the conditional operator or tabs
  • Program has a nested loop

Answer & Explanation Solved by verified expert
3.6 Ratings (566 Votes)
Read the comments and see the output import javautilScanner Package for    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students