// Helene Martin, CSE 142 // Demonstrates usage of for loops and nested for loops public class LoopExamples { public static void main(String[] args) { squares(); blastoff(); square(); mult(); triangle(); numTriangle(); sequence(); } public static void squares() { for (int i = 1; i <= 6; i++) { System.out.println(i + " squared = " + i * i); } } // 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 >= 1; i--) { System.out.print(i + ", "); } System.out.print("blastoff!"); } /* display the following grid of stars using for loops: ********** ********** ********** ********** ********** */ public static void square() { for (int i = 1; i <= 5; i++) { //System.out.println("**********"); for (int j = 1; j <= 10; j++) { System.out.print("*"); } System.out.println(); } } // display a multiplication table public static void mult() { for (int i = 1; i <= 27; i++) { for (int j = 1; j <= 10; j++) { System.out.print(i * j + "\t"); } System.out.println(); } } /* display the following triangle: * 1 ** 2 *** 3 **** 4 ***** 5 */ public static void triangle() { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } } /* 1 22 333 4444 55555 */ public static void numTriangle() { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print(i); } System.out.println(); } } /* Mapping loop counter to number sequences 17 13 9 5 1 count value count * -4 count * -4 + 21 1 17 -4 17 2 13 -8 13 3 9 -12 9 4 5 -16 5 */ public static void sequence() { for (int count = 1; count <= 4; count++) { System.out.print(count * -4 + 21 + " "); } } }