public class NestedForLoops { public static void main(String[] args) { square(); mult(); pyramid(); dotFigure(); } /* display the following grid of stars using for loops: ********** ********** ********** ********** ********** */ public static void square() { for (int i = 1; i <= 5; i++) { // this outer loop repeats the number of lines // System.out.println("**********"); for (int j = 1; j <= 10; j++) { // this inner loop repeats the number of stars on a line System.out.print("*"); } System.out.println(); // this blank println ends the line so the next time through the // loop when i is updated, the star appear on the following line } } /* 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); } } }