Question: Write a program using a Scanner that asks the user for a number n between 1 and 9 (inclusive). The program prints a triangle with

Write a program using a Scanner that asks the user for a number n between 1 and 9 (inclusive). The program prints a triangle with 2n - 1 rows. The first row contains only the square of 1, and it is right-justified. The second row contains the square 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. Starting at row n + 1, the squares between (n - 1) to 1 are printed, again right justified. Row n + 2 prints the squares between (n -2) to 1.

output:

Write a program using a Scanner that asks the user for a

How to fix my codes?

import java.util.*; public class TriangleOfSquares {

public static void main(String[] args) { Scanner input = new Scanner(System.in); //prompt the user to enter the number System.out.print("Enter the number between 1 and 9 inclusive: "); //read the input int n = input.nextInt(); for (int i = 1; i 0; i--) { for (int j = n; j>0; j--) { System.out.print(" "); } for (int j = 1; j If the user enters 7. the following 13-row triangle is printed 41 941 16 9 41 25 16 9 41 36 25 16 9 4 1 49 36 25 16 9 4 1 36 25 16 9 41 25 16 9 41 16 9 41 941 41

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!