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:

JavaScript

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:

JavaScript

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

JavaScript

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:

JavaScript

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

JavaScript

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