public class ForLoops { public static void main(String[] args) { squares(); blastoff(); square(); mult(); pyramid(); dotFigure(); } /* 1 squared = 1 2 squared = 4 3 squared = 9 4 squared = 16 5 squared = 25 6 squared = 36 */ public static void squares() { for (int i = 1; i <= 6; i++) { System.out.println(i + " squared = " + i * i); } System.out.println(); } // display the following text using a loop: // T-minus 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, blastoff! public static void blastoff() { System.out.print("T-minus "); for (int i = 10; i > 0; i--) { System.out.print(i + ", "); } System.out.println("blastoff!"); System.out.println(); } /* display the following grid of stars using for loops: ********** ********** ********** ********** ********** */ public static void square() { for (int i = 1; i <= 2; i++) { // System.out.println("**********"); for (int j = 1; j <= 2; j++) { System.out.print("*"); } System.out.println(); } } /* display a multiplication table 1 2 3 4 ... 2 4 6 8 ... 3 6 9 12... .............. .............. */ public static void mult() { for (int row = 1; row <= 10; row++) { for (int col = 1; col <= 10; col++) { System.out.print(row * col + "\t"); } System.out.println(); } } /* display a pyramid of stars * ** *** **** ***** */ public static void pyramid() { for (int line = 1; line <= 5; line++) { for (int stars = 1; stars <= line; stars++) { System.out.print("*"); } System.out.println(); } } /* ....1 ...2 ..3 .4 5 */ /* line dots -1 * line -1 * line + 5 1 4 -1 4 2 3 -2 3 3 2 -3 2 4 1 -4 1 5 0 -5 0 */ public static void dotFigure() { for (int line = 1; line <= 5; line++) { for (int dots = 1; dots <= -1 * line + 5; dots++) { System.out.print("."); } System.out.println(line); } } }