Skip to content
Advertisement

Printing a squares triangle. How to mirror numbers?

So I’ve been working on this lab for a while now for my programming class and so far I think I’m on the right track.

However, I’m not quite sure how to mirror the numbers. So pretty much, my code is only printing the top half of the triangle. Anyway here is the actual assignment that was given to us:

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 n 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. Assuming the user enters 4, the program prints the following triangle to the console:

          1
       4  1
    9  4  1
16  9  4  1
    9  4  1
       4  1
          1

For full credit, each column should be 3 characters wide and the values should be right justified.

Now here is what I have written for my code so far:

import java.util.Scanner;
public class lab6 {
    public static void main(String[] args) {
        Scanner kybd = new Scanner(System.in);
        System.out.println(
                "Enter a number that is between 1 and 9 (inclusive): ");

        // this is the value that the user will enter for # of rows
        int rows = kybd.nextInt();

        for (int i = rows; i > 0; i--) {
            for (int j = rows; j > 0; j--)
                System.out.print((rows - j + 1) < i ?
                        "   " : String.format("%3d", j * j));
            System.out.println();
        }
    }
}

And this is what that code PRINTS when I enter 4:

Enter a number that is between 1 and 9 (inclusive): 
4
          1
       4  1
    9  4  1
16  9  4  1

As you can see, I can only get the TOP half of the triangle to print out. I’ve been playing around trying to figure out how to mirror it but I can’t seem to figure it out. I’ve looked on this website for help, and all over the Internet but I can’t seem to do it.

Advertisement

Answer

Answer is:

public static void main(String... args) {
    Scanner kybd = new Scanner(System.in);
    System.out.println("Enter a number that is between 1 and 9 (inclusive): ");

    int rows = kybd.nextInt(); // this is the value that the user will enter for # of rows

    for (int i = -rows + 1; i < rows; i++) {
        for (int j = -rows; j < 0; j++)
            System.out.print(abs(i) > j + rows ? "   " : String.format("%3d", j * j));
        System.out.println();
    }
}

Try think of this as how to find points(carthesians) that are betwean three linear functions(area of triangle that lied betwean):

y = 0 // in loops i is y and j is x
y = x + 4
y = -x -4

And here is example result for 4:

enter image description here

And 9:

enter image description here

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement