Control Flow
Nested Loops
Loops Inside Loops
You're printing a 10×10 multiplication table. For each row (1-10), you need to print 10 products (row × 1, row × 2, ... row × 10). The outer loop picks the row; the inner loop fills each column.
Multiplication table
Generate a multiplication table using nested loops.
public class Multiplication {
public static void main(String[] args) {
int size = 5;
// Print header
System.out.print(" ");
for (int i = 1; i <= size; i++) {
System.out.printf("%4d", i);
}
System.out.println();
System.out.println(" " + "-".repeat(size * 4));
// Print table
for (int row = 1; row <= size; row++) {
System.out.printf("%2d |", row);
for (int col = 1; col <= size; col++) {
System.out.printf("%4d", row * col);
}
System.out.println();
}
}
}
public class Multiplication {
public static void main(String[] args) {
int size = 3;
// Print header
System.out.print(" ");
for (int i = 1; i <= size; i++) {
System.out.printf("%4d", i);
}
System.out.println();
System.out.println(" " + "-".repeat(size * 4));
// Print table
for (int row = 1; row <= size; row++) {
System.out.printf("%2d |", row);
for (int col = 1; col <= size; col++) {
System.out.printf("%4d", row * col);
}
System.out.println();
}
}
}
public class Multiplication {
public static void main(String[] args) {
int size = 10;
// Print header
System.out.print(" ");
for (int i = 1; i <= size; i++) {
System.out.printf("%4d", i);
}
System.out.println();
System.out.println(" " + "-".repeat(size * 4));
// Print table
for (int row = 1; row <= size; row++) {
System.out.printf("%2d |", row);
for (int col = 1; col <= size; col++) {
System.out.printf("%4d", row * col);
}
System.out.println();
}
}
}
size ← 5
1public class Multiplication {2 public static void main(String[] args) {3 int size→ 5 = 5; //@size=3, 104 5 // Print header6 System.out.print(" ");7 for (int i = 1; i <= size; i++) {outputfor (int i = 1; i <= size; i++)
pass 1 of 56System.out.print(" ");7for (int i1 = 1; i <= size5; i++) {8 System.out.printf("%4d", i1);9}All 5 passes — pass 1 is the card above pass i1 1 2 2 3 3 4 4 5 5 System.out.println(" " + "-".repeat(size * 4));
9}10System.out.println();11System.out.println(" " + "-".repeat(size5 * 4));output --------------------for (int row = 1; row <= size; row++)
pass 1 of 513// Print table14for (int row1 = 1; row <= size5; row++) { //#?outer_loop15 System.out.printf("%2d |", row1);16 for (int col = 1; col <= size; col++) { //#?inner_loopAll 5 passes — pass 1 is the card above pass row1 1 2 2 3 3 4 4 5 5 for (int col = 1; col <= size; col++)
pass 1 of 2515System.out.printf("%2d |", row);16for (int col1 = 1; col <= size5; col++) { //#?inner_loop17 System.out.printf("%4d", row1 * col1);18}25 passes — pass 1 is the card above pass colrow1 1 1 2 2 1 3 3 1 4 4 1 5 5 1 6 1 2 7 2 2 8 3 2 9 4 2 ⋯ 14 more passes ⋯ 24 4 5 25 5 5 System.out.println();
18 }19 System.out.println();20}System.out.println();
18 }19 System.out.println();20}System.out.println();
18 }19 System.out.println();20}System.out.println();
18 }19 System.out.println();20}System.out.println();
18 }19 System.out.println();20}
size ← 3
1public class Multiplication {2 public static void main(String[] args) {3 int size→ 3 = 3;4 5 // Print header6 System.out.print(" ");7 for (int i = 1; i <= size; i++) {outputfor (int i = 1; i <= size; i++)
pass 1 of 36System.out.print(" ");7for (int i1 = 1; i <= size3; i++) {8 System.out.printf("%4d", i1);9}All 3 passes — pass 1 is the card above pass i1 1 2 2 3 3 System.out.println(" " + "-".repeat(size * 4));
9}10System.out.println();11System.out.println(" " + "-".repeat(size3 * 4));output ------------for (int row = 1; row <= size; row++)
pass 1 of 313// Print table14for (int row1 = 1; row <= size3; row++) {15 System.out.printf("%2d |", row1);16 for (int col = 1; col <= size; col++) {All 3 passes — pass 1 is the card above pass row1 1 2 2 3 3 for (int col = 1; col <= size; col++)
pass 1 of 915System.out.printf("%2d |", row);16for (int col1 = 1; col <= size3; col++) {17 System.out.printf("%4d", row1 * col1);18}All 9 passes — pass 1 is the card above pass colrow1 1 1 2 2 1 3 3 1 4 1 2 5 2 2 6 3 2 7 1 3 8 2 3 9 3 3 System.out.println();
18 }19 System.out.println();20}System.out.println();
18 }19 System.out.println();20}System.out.println();
18 }19 System.out.println();20}
size ← 10
1public class Multiplication {2 public static void main(String[] args) {3 int size→ 10 = 10;4 5 // Print header6 System.out.print(" ");7 for (int i = 1; i <= size; i++) {outputfor (int i = 1; i <= size; i++)
pass 1 of 106System.out.print(" ");7for (int i1 = 1; i <= size10; i++) {8 System.out.printf("%4d", i1);9}All 10 passes — pass 1 is the card above pass i1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 System.out.println(" " + "-".repeat(size * 4));
9}10System.out.println();11System.out.println(" " + "-".repeat(size10 * 4));output ----------------------------------------for (int row = 1; row <= size; row++)
pass 1 of 1013// Print table14for (int row1 = 1; row <= size10; row++) {15 System.out.printf("%2d |", row1);16 for (int col = 1; col <= size; col++) {All 10 passes — pass 1 is the card above pass row1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 for (int col = 1; col <= size; col++)
pass 1 of 10015System.out.printf("%2d |", row);16for (int col1 = 1; col <= size10; col++) {17 System.out.printf("%4d", row1 * col1);18}100 passes — pass 1 is the card above pass colrow1 1 1 2 2 1 3 3 1 4 4 1 5 5 1 6 6 1 7 7 1 8 8 1 9 9 1 ⋯ 89 more passes ⋯ 99 9 10 100 10 10 System.out.println();
18 }19 System.out.println();20}System.out.println();
18 }19 System.out.println();20}System.out.println();
18 }19 System.out.println();20}System.out.println();
18 }19 System.out.println();20}System.out.println();
18 }19 System.out.println();20}System.out.println();
18 }19 System.out.println();20}System.out.println();
18 }19 System.out.println();20}System.out.println();
18 }19 System.out.println();20}System.out.println();
18 }19 System.out.println();20}System.out.println();
18 }19 System.out.println();20}
Outer loop: each row (1-10). Inner loop: each column (multiply by 1-10).
Print rectangle of stars
Draw a rectangle pattern with asterisks.
public class Rectangle {
public static void main(String[] args) {
int width = 5;
int height = 3;
System.out.println("Rectangle " + width + "x" + height + ":");
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
System.out.print("* ");
}
System.out.println(); // New line after each row
}
// Hollow rectangle
System.out.println("\nHollow rectangle:");
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
public class Rectangle {
public static void main(String[] args) {
int width = 3;
int height = 3;
System.out.println("Rectangle " + width + "x" + height + ":");
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
System.out.print("* ");
}
System.out.println(); // New line after each row
}
// Hollow rectangle
System.out.println("\nHollow rectangle:");
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
public class Rectangle {
public static void main(String[] args) {
int width = 8;
int height = 3;
System.out.println("Rectangle " + width + "x" + height + ":");
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
System.out.print("* ");
}
System.out.println(); // New line after each row
}
// Hollow rectangle
System.out.println("\nHollow rectangle:");
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
public class Rectangle {
public static void main(String[] args) {
int width = 5;
int height = 2;
System.out.println("Rectangle " + width + "x" + height + ":");
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
System.out.print("* ");
}
System.out.println(); // New line after each row
}
// Hollow rectangle
System.out.println("\nHollow rectangle:");
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
public class Rectangle {
public static void main(String[] args) {
int width = 5;
int height = 5;
System.out.println("Rectangle " + width + "x" + height + ":");
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
System.out.print("* ");
}
System.out.println(); // New line after each row
}
// Hollow rectangle
System.out.println("\nHollow rectangle:");
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
width ← 5, height ← 3
1public class Rectangle {2 public static void main(String[] args) {3 int width→ 5 = 5; //@width=3, 84 int height→ 3 = 3; //@height=2, 55 6 System.out.println("Rectangle " + width5 + "x" + height3 + ":");outputRectangle 5x3:for (int row = 0; row < height; row++)
pass 1 of 38for (int row0 = 0; row < height3; row++) {9 for (int col = 0; col < width; col++) {All 3 passes — pass 1 is the card above pass row1 0 2 1 3 2 for (int col = 0; col < width; col++)
pass 1 of 158for (int row = 0; row < height; row++) {9 for (int col0 = 0; col < width5; col++) {10 System.out.print("* ");11 }output*15 passes — pass 1 is the card above pass col1 0 2 1 3 2 4 3 5 4 6 0 7 1 8 2 9 3 ⋯ 4 more passes ⋯ 14 3 15 4 System.out.println(); // New line after each row
11 }12 System.out.println(); // New line after each row13}System.out.println(); // New line after each row
11 }12 System.out.println(); // New line after each row13}System.out.println(); // New line after each row
11 }12 System.out.println(); // New line after each row13}System.out.println(" Hollow rectangle:");
15// Hollow rectangle16System.out.println("\nHollow rectangle:");17for (int row = 0; row < height; row++) {output Hollow rectangle:for (int row = 0; row < height; row++)
pass 1 of 316System.out.println("\nHollow rectangle:");17for (int row0 = 0; row < height3; row++) {18 for (int col = 0; col < width; col++) {All 3 passes — pass 1 is the card above pass row1 0 2 1 3 2 for (int col = 0; col < width; col++)
pass 1 of 1517for (int row = 0; row < height; row++) {18 for (int col0 = 0; col < width5; col++) {19 if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {15 passes — pass 1 is the card above pass col1 0 2 1 3 2 4 3 5 4 6 0 7 1 8 2 9 3 ⋯ 4 more passes ⋯ 14 3 15 4 if (row == 0 || row == height - 1 || col == 0 || col == width - 1)
pass 1 of 1218for (int col = 0; col < width; col++) {19 if (row0 == 0 || row == height3 - 1 || col0 == 0 || col == width5 - 1) {20 System.out.print("* ");21 } else {output*All 12 passes — pass 1 is the card above pass rowcol1 0 0 2 0 1 3 0 2 4 0 3 5 0 4 6 1 0 7 1 4 8 2 0 9 2 1 10 2 2 11 2 3 12 2 4 System.out.println();
24 }25 System.out.println();26}else
pass 1 of 320 System.out.print("* ");21} else {22 System.out.print(" ");23}outputSystem.out.println();
24 }25 System.out.println();26}System.out.println();
24 }25 System.out.println();26}
width ← 3, height ← 3
1public class Rectangle {2 public static void main(String[] args) {3 int width→ 3 = 3;4 int height→ 3 = 3;5 6 System.out.println("Rectangle " + width3 + "x" + height3 + ":");outputRectangle 3x3:for (int row = 0; row < height; row++)
pass 1 of 38for (int row0 = 0; row < height3; row++) {9 for (int col = 0; col < width; col++) {All 3 passes — pass 1 is the card above pass row1 0 2 1 3 2 for (int col = 0; col < width; col++)
pass 1 of 98for (int row = 0; row < height; row++) {9 for (int col0 = 0; col < width3; col++) {10 System.out.print("* ");11 }output*All 9 passes — pass 1 is the card above pass col1 0 2 1 3 2 4 0 5 1 6 2 7 0 8 1 9 2 System.out.println(); // New line after each row
11 }12 System.out.println(); // New line after each row13}System.out.println(); // New line after each row
11 }12 System.out.println(); // New line after each row13}System.out.println(); // New line after each row
11 }12 System.out.println(); // New line after each row13}System.out.println(" Hollow rectangle:");
15// Hollow rectangle16System.out.println("\nHollow rectangle:");17for (int row = 0; row < height; row++) {output Hollow rectangle:for (int row = 0; row < height; row++)
pass 1 of 316System.out.println("\nHollow rectangle:");17for (int row0 = 0; row < height3; row++) {18 for (int col = 0; col < width; col++) {All 3 passes — pass 1 is the card above pass row1 0 2 1 3 2 for (int col = 0; col < width; col++)
pass 1 of 917for (int row = 0; row < height; row++) {18 for (int col0 = 0; col < width3; col++) {19 if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {All 9 passes — pass 1 is the card above pass col1 0 2 1 3 2 4 0 5 1 6 2 7 0 8 1 9 2 if (row == 0 || row == height - 1 || col == 0 || col == width - 1)
pass 1 of 818for (int col = 0; col < width; col++) {19 if (row0 == 0 || row == height3 - 1 || col0 == 0 || col == width3 - 1) {20 System.out.print("* ");21 } else {output*All 8 passes — pass 1 is the card above pass rowcol1 0 0 2 0 1 3 0 2 4 1 0 5 1 2 6 2 0 7 2 1 8 2 2 System.out.println();
24 }25 System.out.println();26}else
20 System.out.print("* ");21} else {22 System.out.print(" ");23}outputSystem.out.println();
24 }25 System.out.println();26}System.out.println();
24 }25 System.out.println();26}
width ← 8, height ← 3
1public class Rectangle {2 public static void main(String[] args) {3 int width→ 8 = 8;4 int height→ 3 = 3;5 6 System.out.println("Rectangle " + width8 + "x" + height3 + ":");outputRectangle 8x3:for (int row = 0; row < height; row++)
pass 1 of 38for (int row0 = 0; row < height3; row++) {9 for (int col = 0; col < width; col++) {All 3 passes — pass 1 is the card above pass row1 0 2 1 3 2 for (int col = 0; col < width; col++)
pass 1 of 248for (int row = 0; row < height; row++) {9 for (int col0 = 0; col < width8; col++) {10 System.out.print("* ");11 }output*24 passes — pass 1 is the card above pass col1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 0 ⋯ 13 more passes ⋯ 23 6 24 7 System.out.println(); // New line after each row
11 }12 System.out.println(); // New line after each row13}System.out.println(); // New line after each row
11 }12 System.out.println(); // New line after each row13}System.out.println(); // New line after each row
11 }12 System.out.println(); // New line after each row13}System.out.println(" Hollow rectangle:");
15// Hollow rectangle16System.out.println("\nHollow rectangle:");17for (int row = 0; row < height; row++) {output Hollow rectangle:for (int row = 0; row < height; row++)
pass 1 of 316System.out.println("\nHollow rectangle:");17for (int row0 = 0; row < height3; row++) {18 for (int col = 0; col < width; col++) {All 3 passes — pass 1 is the card above pass row1 0 2 1 3 2 for (int col = 0; col < width; col++)
pass 1 of 2417for (int row = 0; row < height; row++) {18 for (int col0 = 0; col < width8; col++) {19 if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {24 passes — pass 1 is the card above pass col1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 0 ⋯ 13 more passes ⋯ 23 6 24 7 if (row == 0 || row == height - 1 || col == 0 || col == width - 1)
pass 1 of 1818for (int col = 0; col < width; col++) {19 if (row0 == 0 || row == height3 - 1 || col0 == 0 || col == width8 - 1) {20 System.out.print("* ");21 } else {output*18 passes — pass 1 is the card above pass rowcol1 0 0 2 0 1 3 0 2 4 0 3 5 0 4 6 0 5 7 0 6 8 0 7 9 1 0 ⋯ 7 more passes ⋯ 17 2 6 18 2 7 System.out.println();
24 }25 System.out.println();26}else
pass 1 of 620 System.out.print("* ");21} else {22 System.out.print(" ");23}outputSystem.out.println();
24 }25 System.out.println();26}System.out.println();
24 }25 System.out.println();26}
width ← 5, height ← 2
1public class Rectangle {2 public static void main(String[] args) {3 int width→ 5 = 5;4 int height→ 2 = 2;5 6 System.out.println("Rectangle " + width5 + "x" + height2 + ":");outputRectangle 5x2:for (int row = 0; row < height; row++)
pass 1 of 28for (int row0 = 0; row < height2; row++) {9 for (int col = 0; col < width; col++) {for (int col = 0; col < width; col++)
pass 1 of 108for (int row = 0; row < height; row++) {9 for (int col0 = 0; col < width5; col++) {10 System.out.print("* ");11 }output*All 10 passes — pass 1 is the card above pass col1 0 2 1 3 2 4 3 5 4 6 0 7 1 8 2 9 3 10 4 System.out.println(); // New line after each row
11 }12 System.out.println(); // New line after each row13}for (int row = 0; row < height; row++)
pass 2 of 28for (int row1 = 0; row < height2; row++) {9 for (int col = 0; col < width; col++) {System.out.println(); // New line after each row
11 }12 System.out.println(); // New line after each row13}System.out.println(" Hollow rectangle:");
15// Hollow rectangle16System.out.println("\nHollow rectangle:");17for (int row = 0; row < height; row++) {output Hollow rectangle:for (int row = 0; row < height; row++)
pass 1 of 216System.out.println("\nHollow rectangle:");17for (int row0 = 0; row < height2; row++) {18 for (int col = 0; col < width; col++) {for (int col = 0; col < width; col++)
pass 1 of 1017for (int row = 0; row < height; row++) {18 for (int col0 = 0; col < width5; col++) {19 if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {All 10 passes — pass 1 is the card above pass col1 0 2 1 3 2 4 3 5 4 6 0 7 1 8 2 9 3 10 4 if (row == 0 || row == height - 1 || col == 0 || col == width - 1)
pass 1 of 1018for (int col = 0; col < width; col++) {19 if (row0 == 0 || row == height2 - 1 || col0 == 0 || col == width5 - 1) {20 System.out.print("* ");21 } else {output*All 10 passes — pass 1 is the card above pass rowcol1 0 0 2 0 1 3 0 2 4 0 3 5 0 4 6 1 0 7 1 1 8 1 2 9 1 3 10 1 4 System.out.println();
24 }25 System.out.println();26}for (int row = 0; row < height; row++)
pass 2 of 216System.out.println("\nHollow rectangle:");17for (int row1 = 0; row < height2; row++) {18 for (int col = 0; col < width; col++) {System.out.println();
24 }25 System.out.println();26}
width ← 5, height ← 5
1public class Rectangle {2 public static void main(String[] args) {3 int width→ 5 = 5;4 int height→ 5 = 5;5 6 System.out.println("Rectangle " + width5 + "x" + height5 + ":");outputRectangle 5x5:for (int row = 0; row < height; row++)
pass 1 of 58for (int row0 = 0; row < height5; row++) {9 for (int col = 0; col < width; col++) {All 5 passes — pass 1 is the card above pass row1 0 2 1 3 2 4 3 5 4 for (int col = 0; col < width; col++)
pass 1 of 258for (int row = 0; row < height; row++) {9 for (int col0 = 0; col < width5; col++) {10 System.out.print("* ");11 }output*25 passes — pass 1 is the card above pass col1 0 2 1 3 2 4 3 5 4 6 0 7 1 8 2 9 3 ⋯ 14 more passes ⋯ 24 3 25 4 System.out.println(); // New line after each row
11 }12 System.out.println(); // New line after each row13}System.out.println(); // New line after each row
11 }12 System.out.println(); // New line after each row13}System.out.println(); // New line after each row
11 }12 System.out.println(); // New line after each row13}System.out.println(); // New line after each row
11 }12 System.out.println(); // New line after each row13}System.out.println(); // New line after each row
11 }12 System.out.println(); // New line after each row13}System.out.println(" Hollow rectangle:");
15// Hollow rectangle16System.out.println("\nHollow rectangle:");17for (int row = 0; row < height; row++) {output Hollow rectangle:for (int row = 0; row < height; row++)
pass 1 of 516System.out.println("\nHollow rectangle:");17for (int row0 = 0; row < height5; row++) {18 for (int col = 0; col < width; col++) {All 5 passes — pass 1 is the card above pass row1 0 2 1 3 2 4 3 5 4 for (int col = 0; col < width; col++)
pass 1 of 2517for (int row = 0; row < height; row++) {18 for (int col0 = 0; col < width5; col++) {19 if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {25 passes — pass 1 is the card above pass col1 0 2 1 3 2 4 3 5 4 6 0 7 1 8 2 9 3 ⋯ 14 more passes ⋯ 24 3 25 4 if (row == 0 || row == height - 1 || col == 0 || col == width - 1)
pass 1 of 1618for (int col = 0; col < width; col++) {19 if (row0 == 0 || row == height5 - 1 || col0 == 0 || col == width5 - 1) {20 System.out.print("* ");21 } else {output*16 passes — pass 1 is the card above pass rowcol1 0 0 2 0 1 3 0 2 4 0 3 5 0 4 6 1 0 7 1 4 8 2 0 9 2 4 ⋯ 5 more passes ⋯ 15 4 3 16 4 4 System.out.println();
24 }25 System.out.println();26}else
pass 1 of 920 System.out.print("* ");21} else {22 System.out.print(" ");23}outputSystem.out.println();
24 }25 System.out.println();26}System.out.println();
24 }25 System.out.println();26}System.out.println();
24 }25 System.out.println();26}System.out.println();
24 }25 System.out.println();26}
Outer loop: rows. Inner loop: columns within each row.
Print triangle pattern
Create a right triangle by varying inner loop iterations.
public class Triangle {
public static void main(String[] args) {
int height = 5;
// Right triangle (left-aligned)
System.out.println("Right triangle:");
for (int row = 1; row <= height; row++) {
for (int col = 1; col <= row; col++) {
System.out.print("* ");
}
System.out.println();
}
// Inverted triangle
System.out.println("\nInverted triangle:");
for (int row = height; row >= 1; row--) {
for (int col = 1; col <= row; col++) {
System.out.print("* ");
}
System.out.println();
}
// Right-aligned triangle
System.out.println("\nRight-aligned triangle:");
for (int row = 1; row <= height; row++) {
// Print spaces
for (int space = height - row; space > 0; space--) {
System.out.print(" ");
}
// Print stars
for (int star = 1; star <= row; star++) {
System.out.print("* ");
}
System.out.println();
}
}
}
public class Triangle {
public static void main(String[] args) {
int height = 3;
// Right triangle (left-aligned)
System.out.println("Right triangle:");
for (int row = 1; row <= height; row++) {
for (int col = 1; col <= row; col++) {
System.out.print("* ");
}
System.out.println();
}
// Inverted triangle
System.out.println("\nInverted triangle:");
for (int row = height; row >= 1; row--) {
for (int col = 1; col <= row; col++) {
System.out.print("* ");
}
System.out.println();
}
// Right-aligned triangle
System.out.println("\nRight-aligned triangle:");
for (int row = 1; row <= height; row++) {
// Print spaces
for (int space = height - row; space > 0; space--) {
System.out.print(" ");
}
// Print stars
for (int star = 1; star <= row; star++) {
System.out.print("* ");
}
System.out.println();
}
}
}
public class Triangle {
public static void main(String[] args) {
int height = 7;
// Right triangle (left-aligned)
System.out.println("Right triangle:");
for (int row = 1; row <= height; row++) {
for (int col = 1; col <= row; col++) {
System.out.print("* ");
}
System.out.println();
}
// Inverted triangle
System.out.println("\nInverted triangle:");
for (int row = height; row >= 1; row--) {
for (int col = 1; col <= row; col++) {
System.out.print("* ");
}
System.out.println();
}
// Right-aligned triangle
System.out.println("\nRight-aligned triangle:");
for (int row = 1; row <= height; row++) {
// Print spaces
for (int space = height - row; space > 0; space--) {
System.out.print(" ");
}
// Print stars
for (int star = 1; star <= row; star++) {
System.out.print("* ");
}
System.out.println();
}
}
}
height ← 5
1public class Triangle {2 public static void main(String[] args) {3 int height→ 5 = 5; //@height=3, 74 5 // Right triangle (left-aligned)6 System.out.println("Right triangle:");7 for (int row = 1; row <= height; row++) {outputRight triangle:for (int row = 1; row <= height; row++)
pass 1 of 56System.out.println("Right triangle:");7for (int row1 = 1; row <= height5; row++) {8 for (int col = 1; col <= row; col++) { //#?varying_limitAll 5 passes — pass 1 is the card above pass row1 1 2 2 3 3 4 4 5 5 for (int col = 1; col <= row; col++)
pass 1 of 157for (int row = 1; row <= height; row++) {8 for (int col1 = 1; col <= row1; col++) { //#?varying_limit9 System.out.print("* ");10 }output*15 passes — pass 1 is the card above pass colrow1 1 1 2 1 2 3 2 2 4 1 3 5 2 3 6 3 3 7 1 4 8 2 4 9 3 4 ⋯ 4 more passes ⋯ 14 4 5 15 5 5 System.out.println();
10 }11 System.out.println();12}System.out.println();
10 }11 System.out.println();12}System.out.println();
10 }11 System.out.println();12}System.out.println();
10 }11 System.out.println();12}System.out.println();
10 }11 System.out.println();12}System.out.println(" Inverted triangle:");
14// Inverted triangle15System.out.println("\nInverted triangle:");16for (int row = height; row >= 1; row--) {output Inverted triangle:for (int row = height; row >= 1; row--)
pass 1 of 515System.out.println("\nInverted triangle:");16for (int row5 = height; row >= 1; row--) {17 for (int col = 1; col <= row; col++) {All 5 passes — pass 1 is the card above pass row1 5 2 4 3 3 4 2 5 1 for (int col = 1; col <= row; col++)
pass 1 of 1516for (int row = height; row >= 1; row--) {17 for (int col1 = 1; col <= row5; col++) {18 System.out.print("* ");19 }output*15 passes — pass 1 is the card above pass colrow1 1 5 2 2 5 3 3 5 4 4 5 5 5 5 6 1 4 7 2 4 8 3 4 9 4 4 ⋯ 4 more passes ⋯ 14 2 2 15 1 1 System.out.println();
19 }20 System.out.println();21}System.out.println();
19 }20 System.out.println();21}System.out.println();
19 }20 System.out.println();21}System.out.println();
19 }20 System.out.println();21}System.out.println();
19 }20 System.out.println();21}System.out.println(" Right-aligned triangle:");
23// Right-aligned triangle24System.out.println("\nRight-aligned triangle:");25for (int row = 1; row <= height; row++) {output Right-aligned triangle:for (int row = 1; row <= height; row++)
pass 1 of 524System.out.println("\nRight-aligned triangle:");25for (int row1 = 1; row <= height5; row++) {26 // Print spacesAll 5 passes — pass 1 is the card above pass row1 1 2 2 3 3 4 4 5 5 for (int space = height - row; space > 0; space--)
pass 1 of 1026// Print spaces27for (int space4 = height5 - row1; space > 0; space--) {28 System.out.print(" ");29}outputAll 10 passes — pass 1 is the card above pass spacerow1 4 1 2 3 1 3 2 1 4 1 1 5 3 2 6 2 2 7 1 2 8 2 3 9 1 3 10 1 4 for (int star = 1; star <= row; star++)
pass 1 of 1530// Print stars31for (int star1 = 1; star <= row1; star++) {32 System.out.print("* ");33}output*15 passes — pass 1 is the card above pass starrow1 1 1 2 1 2 3 2 2 4 1 3 5 2 3 6 3 3 7 1 4 8 2 4 9 3 4 ⋯ 4 more passes ⋯ 14 4 5 15 5 5 System.out.println();
33 }34 System.out.println();35}System.out.println();
33 }34 System.out.println();35}System.out.println();
33 }34 System.out.println();35}System.out.println();
33 }34 System.out.println();35}System.out.println();
33 }34 System.out.println();35}
height ← 3
1public class Triangle {2 public static void main(String[] args) {3 int height→ 3 = 3;4 5 // Right triangle (left-aligned)6 System.out.println("Right triangle:");7 for (int row = 1; row <= height; row++) {outputRight triangle:for (int row = 1; row <= height; row++)
pass 1 of 36System.out.println("Right triangle:");7for (int row1 = 1; row <= height3; row++) {8 for (int col = 1; col <= row; col++) {All 3 passes — pass 1 is the card above pass row1 1 2 2 3 3 for (int col = 1; col <= row; col++)
pass 1 of 67for (int row = 1; row <= height; row++) {8 for (int col1 = 1; col <= row1; col++) {9 System.out.print("* ");10 }output*All 6 passes — pass 1 is the card above pass colrow1 1 1 2 1 2 3 2 2 4 1 3 5 2 3 6 3 3 System.out.println();
10 }11 System.out.println();12}System.out.println();
10 }11 System.out.println();12}System.out.println();
10 }11 System.out.println();12}System.out.println(" Inverted triangle:");
14// Inverted triangle15System.out.println("\nInverted triangle:");16for (int row = height; row >= 1; row--) {output Inverted triangle:for (int row = height; row >= 1; row--)
pass 1 of 315System.out.println("\nInverted triangle:");16for (int row3 = height; row >= 1; row--) {17 for (int col = 1; col <= row; col++) {All 3 passes — pass 1 is the card above pass row1 3 2 2 3 1 for (int col = 1; col <= row; col++)
pass 1 of 616for (int row = height; row >= 1; row--) {17 for (int col1 = 1; col <= row3; col++) {18 System.out.print("* ");19 }output*All 6 passes — pass 1 is the card above pass colrow1 1 3 2 2 3 3 3 3 4 1 2 5 2 2 6 1 1 System.out.println();
19 }20 System.out.println();21}System.out.println();
19 }20 System.out.println();21}System.out.println();
19 }20 System.out.println();21}System.out.println(" Right-aligned triangle:");
23// Right-aligned triangle24System.out.println("\nRight-aligned triangle:");25for (int row = 1; row <= height; row++) {output Right-aligned triangle:for (int row = 1; row <= height; row++)
pass 1 of 324System.out.println("\nRight-aligned triangle:");25for (int row1 = 1; row <= height3; row++) {26 // Print spacesAll 3 passes — pass 1 is the card above pass row1 1 2 2 3 3 for (int space = height - row; space > 0; space--)
pass 1 of 326// Print spaces27for (int space2 = height3 - row1; space > 0; space--) {28 System.out.print(" ");29}outputAll 3 passes — pass 1 is the card above pass spacerow1 2 1 2 1 1 3 1 2 for (int star = 1; star <= row; star++)
pass 1 of 630// Print stars31for (int star1 = 1; star <= row1; star++) {32 System.out.print("* ");33}output*All 6 passes — pass 1 is the card above pass starrow1 1 1 2 1 2 3 2 2 4 1 3 5 2 3 6 3 3 System.out.println();
33 }34 System.out.println();35}System.out.println();
33 }34 System.out.println();35}System.out.println();
33 }34 System.out.println();35}
height ← 7
1public class Triangle {2 public static void main(String[] args) {3 int height→ 7 = 7;4 5 // Right triangle (left-aligned)6 System.out.println("Right triangle:");7 for (int row = 1; row <= height; row++) {outputRight triangle:for (int row = 1; row <= height; row++)
pass 1 of 76System.out.println("Right triangle:");7for (int row1 = 1; row <= height7; row++) {8 for (int col = 1; col <= row; col++) {All 7 passes — pass 1 is the card above pass row1 1 2 2 3 3 4 4 5 5 6 6 7 7 for (int col = 1; col <= row; col++)
pass 1 of 287for (int row = 1; row <= height; row++) {8 for (int col1 = 1; col <= row1; col++) {9 System.out.print("* ");10 }output*28 passes — pass 1 is the card above pass colrow1 1 1 2 1 2 3 2 2 4 1 3 5 2 3 6 3 3 7 1 4 8 2 4 9 3 4 ⋯ 17 more passes ⋯ 27 6 7 28 7 7 System.out.println();
10 }11 System.out.println();12}System.out.println();
10 }11 System.out.println();12}System.out.println();
10 }11 System.out.println();12}System.out.println();
10 }11 System.out.println();12}System.out.println();
10 }11 System.out.println();12}System.out.println();
10 }11 System.out.println();12}System.out.println();
10 }11 System.out.println();12}System.out.println(" Inverted triangle:");
14// Inverted triangle15System.out.println("\nInverted triangle:");16for (int row = height; row >= 1; row--) {output Inverted triangle:for (int row = height; row >= 1; row--)
pass 1 of 715System.out.println("\nInverted triangle:");16for (int row7 = height; row >= 1; row--) {17 for (int col = 1; col <= row; col++) {All 7 passes — pass 1 is the card above pass row1 7 2 6 3 5 4 4 5 3 6 2 7 1 for (int col = 1; col <= row; col++)
pass 1 of 2816for (int row = height; row >= 1; row--) {17 for (int col1 = 1; col <= row7; col++) {18 System.out.print("* ");19 }output*28 passes — pass 1 is the card above pass colrow1 1 7 2 2 7 3 3 7 4 4 7 5 5 7 6 6 7 7 7 7 8 1 6 9 2 6 ⋯ 17 more passes ⋯ 27 2 2 28 1 1 System.out.println();
19 }20 System.out.println();21}System.out.println();
19 }20 System.out.println();21}System.out.println();
19 }20 System.out.println();21}System.out.println();
19 }20 System.out.println();21}System.out.println();
19 }20 System.out.println();21}System.out.println();
19 }20 System.out.println();21}System.out.println();
19 }20 System.out.println();21}System.out.println(" Right-aligned triangle:");
23// Right-aligned triangle24System.out.println("\nRight-aligned triangle:");25for (int row = 1; row <= height; row++) {output Right-aligned triangle:for (int row = 1; row <= height; row++)
pass 1 of 724System.out.println("\nRight-aligned triangle:");25for (int row1 = 1; row <= height7; row++) {26 // Print spacesAll 7 passes — pass 1 is the card above pass row1 1 2 2 3 3 4 4 5 5 6 6 7 7 for (int space = height - row; space > 0; space--)
pass 1 of 2126// Print spaces27for (int space6 = height7 - row1; space > 0; space--) {28 System.out.print(" ");29}output21 passes — pass 1 is the card above pass spacerow1 6 1 2 5 1 3 4 1 4 3 1 5 2 1 6 1 1 7 5 2 8 4 2 9 3 2 ⋯ 10 more passes ⋯ 20 1 5 21 1 6 for (int star = 1; star <= row; star++)
pass 1 of 2830// Print stars31for (int star1 = 1; star <= row1; star++) {32 System.out.print("* ");33}output*28 passes — pass 1 is the card above pass starrow1 1 1 2 1 2 3 2 2 4 1 3 5 2 3 6 3 3 7 1 4 8 2 4 9 3 4 ⋯ 17 more passes ⋯ 27 6 7 28 7 7 System.out.println();
33 }34 System.out.println();35}System.out.println();
33 }34 System.out.println();35}System.out.println();
33 }34 System.out.println();35}System.out.println();
33 }34 System.out.println();35}System.out.println();
33 }34 System.out.println();35}System.out.println();
33 }34 System.out.println();35}System.out.println();
33 }34 System.out.println();35}
The inner loop's limit depends on the outer loop variable.
Search in 2D grid
Find a value in a 2D array (matrix).
public class GridSearch {
public static void main(String[] args) {
int[][] grid = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int target = 7;
// Print the grid
System.out.println("Grid:");
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
System.out.printf("%3d", grid[row][col]);
}
System.out.println();
}
// Search for target
int foundRow = -1;
int foundCol = -1;
search:
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
if (grid[row][col] == target) {
foundRow = row;
foundCol = col;
break search;
}
}
}
if (foundRow != -1) {
System.out.println("\nFound " + target + " at [" + foundRow + "][" + foundCol + "]");
} else {
System.out.println("\n" + target + " not found in grid");
}
}
}
public class GridSearch {
public static void main(String[] args) {
int[][] grid = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int target = 1;
// Print the grid
System.out.println("Grid:");
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
System.out.printf("%3d", grid[row][col]);
}
System.out.println();
}
// Search for target
int foundRow = -1;
int foundCol = -1;
search:
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
if (grid[row][col] == target) {
foundRow = row;
foundCol = col;
break search;
}
}
}
if (foundRow != -1) {
System.out.println("\nFound " + target + " at [" + foundRow + "][" + foundCol + "]");
} else {
System.out.println("\n" + target + " not found in grid");
}
}
}
public class GridSearch {
public static void main(String[] args) {
int[][] grid = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int target = 12;
// Print the grid
System.out.println("Grid:");
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
System.out.printf("%3d", grid[row][col]);
}
System.out.println();
}
// Search for target
int foundRow = -1;
int foundCol = -1;
search:
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
if (grid[row][col] == target) {
foundRow = row;
foundCol = col;
break search;
}
}
}
if (foundRow != -1) {
System.out.println("\nFound " + target + " at [" + foundRow + "][" + foundCol + "]");
} else {
System.out.println("\n" + target + " not found in grid");
}
}
}
public class GridSearch {
public static void main(String[] args) {
int[][] grid = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int target = 99;
// Print the grid
System.out.println("Grid:");
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
System.out.printf("%3d", grid[row][col]);
}
System.out.println();
}
// Search for target
int foundRow = -1;
int foundCol = -1;
search:
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[row].length; col++) {
if (grid[row][col] == target) {
foundRow = row;
foundCol = col;
break search;
}
}
}
if (foundRow != -1) {
System.out.println("\nFound " + target + " at [" + foundRow + "][" + foundCol + "]");
} else {
System.out.println("\n" + target + " not found in grid");
}
}
}
target ← 7
1public class GridSearch {2 public static void main(String[] args) {3 int[][] grid = {4 {1, 2, 3, 4},5 {5, 6, 7, 8},6 {9, 10, 11, 12}7 };8 int target→ 7 = 7; //@target=1, 12, 999 10 // Print the grid11 System.out.println("Grid:");12 for (int row = 0; row < grid.length; row++) {outputGrid:for (int row = 0; row < grid.length; row++)
pass 1 of 311System.out.println("Grid:");12for (int row0 = 0; row < grid.length3; row++) {13 for (int col = 0; col < grid[row].length; col++) {All 3 passes — pass 1 is the card above pass row1 0 2 1 3 2 for (int col = 0; col < grid[row].length; col++)
pass 1 of 1212for (int row = 0; row < grid.length; row++) {13 for (int col0 = 0; col < grid[row].length4; col++) {14 System.out.printf("%3d", grid[row][col]1);15 }All 12 passes — pass 1 is the card above pass colrowgrid[row][col]1 0 0 1 2 1 0 2 3 2 0 3 4 3 0 4 5 0 1 5 6 1 1 6 7 2 1 7 8 3 1 8 9 0 2 9 10 1 2 10 11 2 2 11 12 3 2 12 System.out.println();
15 }16 System.out.println();17}System.out.println();
15 }16 System.out.println();17}System.out.println();
15 }16 System.out.println();17}foundRow ← -1, foundCol ← -1
19// Search for target20int foundRow→ -1 = -1;21int foundCol→ -1 = -1;if (foundRow != -1)
34if (foundRow1 != -1) {35 System.out.println("\nFound " + target7 + " at [" + foundRow1 + "][" + foundCol2 + "]");36} else {output Found 7 at [1][2]
target ← 1
1public class GridSearch {2 public static void main(String[] args) {3 int[][] grid = {4 {1, 2, 3, 4},5 {5, 6, 7, 8},6 {9, 10, 11, 12}7 };8 int target→ 1 = 1;9 10 // Print the grid11 System.out.println("Grid:");12 for (int row = 0; row < grid.length; row++) {outputGrid:for (int row = 0; row < grid.length; row++)
pass 1 of 311System.out.println("Grid:");12for (int row0 = 0; row < grid.length3; row++) {13 for (int col = 0; col < grid[row].length; col++) {All 3 passes — pass 1 is the card above pass row1 0 2 1 3 2 for (int col = 0; col < grid[row].length; col++)
pass 1 of 1212for (int row = 0; row < grid.length; row++) {13 for (int col0 = 0; col < grid[row].length4; col++) {14 System.out.printf("%3d", grid[row][col]1);15 }All 12 passes — pass 1 is the card above pass colrowgrid[row][col]1 0 0 1 2 1 0 2 3 2 0 3 4 3 0 4 5 0 1 5 6 1 1 6 7 2 1 7 8 3 1 8 9 0 2 9 10 1 2 10 11 2 2 11 12 3 2 12 System.out.println();
15 }16 System.out.println();17}System.out.println();
15 }16 System.out.println();17}System.out.println();
15 }16 System.out.println();17}foundRow ← -1, foundCol ← -1
19// Search for target20int foundRow→ -1 = -1;21int foundCol→ -1 = -1;if (foundRow != -1)
34if (foundRow0 != -1) {35 System.out.println("\nFound " + target1 + " at [" + foundRow0 + "][" + foundCol0 + "]");36} else {output Found 1 at [0][0]
target ← 12
1public class GridSearch {2 public static void main(String[] args) {3 int[][] grid = {4 {1, 2, 3, 4},5 {5, 6, 7, 8},6 {9, 10, 11, 12}7 };8 int target→ 12 = 12;9 10 // Print the grid11 System.out.println("Grid:");12 for (int row = 0; row < grid.length; row++) {outputGrid:for (int row = 0; row < grid.length; row++)
pass 1 of 311System.out.println("Grid:");12for (int row0 = 0; row < grid.length3; row++) {13 for (int col = 0; col < grid[row].length; col++) {All 3 passes — pass 1 is the card above pass row1 0 2 1 3 2 for (int col = 0; col < grid[row].length; col++)
pass 1 of 1212for (int row = 0; row < grid.length; row++) {13 for (int col0 = 0; col < grid[row].length4; col++) {14 System.out.printf("%3d", grid[row][col]1);15 }All 12 passes — pass 1 is the card above pass colrowgrid[row][col]1 0 0 1 2 1 0 2 3 2 0 3 4 3 0 4 5 0 1 5 6 1 1 6 7 2 1 7 8 3 1 8 9 0 2 9 10 1 2 10 11 2 2 11 12 3 2 12 System.out.println();
15 }16 System.out.println();17}System.out.println();
15 }16 System.out.println();17}System.out.println();
15 }16 System.out.println();17}foundRow ← -1, foundCol ← -1
19// Search for target20int foundRow→ -1 = -1;21int foundCol→ -1 = -1;if (foundRow != -1)
34if (foundRow2 != -1) {35 System.out.println("\nFound " + target12 + " at [" + foundRow2 + "][" + foundCol3 + "]");36} else {output Found 12 at [2][3]
target ← 99
1public class GridSearch {2 public static void main(String[] args) {3 int[][] grid = {4 {1, 2, 3, 4},5 {5, 6, 7, 8},6 {9, 10, 11, 12}7 };8 int target→ 99 = 99;9 10 // Print the grid11 System.out.println("Grid:");12 for (int row = 0; row < grid.length; row++) {outputGrid:for (int row = 0; row < grid.length; row++)
pass 1 of 311System.out.println("Grid:");12for (int row0 = 0; row < grid.length3; row++) {13 for (int col = 0; col < grid[row].length; col++) {All 3 passes — pass 1 is the card above pass row1 0 2 1 3 2 for (int col = 0; col < grid[row].length; col++)
pass 1 of 1212for (int row = 0; row < grid.length; row++) {13 for (int col0 = 0; col < grid[row].length4; col++) {14 System.out.printf("%3d", grid[row][col]1);15 }All 12 passes — pass 1 is the card above pass colrowgrid[row][col]1 0 0 1 2 1 0 2 3 2 0 3 4 3 0 4 5 0 1 5 6 1 1 6 7 2 1 7 8 3 1 8 9 0 2 9 10 1 2 10 11 2 2 11 12 3 2 12 System.out.println();
15 }16 System.out.println();17}System.out.println();
15 }16 System.out.println();17}System.out.println();
15 }16 System.out.println();17}foundRow ← -1, foundCol ← -1
19// Search for target20int foundRow→ -1 = -1;21int foundCol→ -1 = -1;else
35 System.out.println("\nFound " + target + " at [" + foundRow + "][" + foundCol + "]");36} else {37 System.out.println("\n" + target99 + " not found in grid");38}output 99 not found in grid
Nested loops to traverse all [row][col] positions.
Compare all pairs
Find all pairs that satisfy a condition.
public class Pairs {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40};
int targetSum = 50;
System.out.println("Find pairs that sum to " + targetSum + ":");
// All pairs (including same index)
System.out.println("\nAll pairs with sum " + targetSum + ":");
for (int i = 0; i < numbers.length; i++) {
for (int j = i + 1; j < numbers.length; j++) {
int sum = numbers[i] + numbers[j];
if (sum == targetSum) {
System.out.println(" " + numbers[i] + " + " + numbers[j] + " = " + targetSum);
}
}
}
// All possible pairs (cartesian product)
System.out.println("\nAll pairs (order matters):");
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers.length; j++) {
if (i != j) {
System.out.println(" (" + numbers[i] + ", " + numbers[j] + ")");
}
}
}
}
}
public class Pairs {
public static void main(String[] args) {
int[] numbers = {5, 10, 15, 20, 25};
int targetSum = 50;
System.out.println("Find pairs that sum to " + targetSum + ":");
// All pairs (including same index)
System.out.println("\nAll pairs with sum " + targetSum + ":");
for (int i = 0; i < numbers.length; i++) {
for (int j = i + 1; j < numbers.length; j++) {
int sum = numbers[i] + numbers[j];
if (sum == targetSum) {
System.out.println(" " + numbers[i] + " + " + numbers[j] + " = " + targetSum);
}
}
}
// All possible pairs (cartesian product)
System.out.println("\nAll pairs (order matters):");
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers.length; j++) {
if (i != j) {
System.out.println(" (" + numbers[i] + ", " + numbers[j] + ")");
}
}
}
}
}
public class Pairs {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40};
int targetSum = 30;
System.out.println("Find pairs that sum to " + targetSum + ":");
// All pairs (including same index)
System.out.println("\nAll pairs with sum " + targetSum + ":");
for (int i = 0; i < numbers.length; i++) {
for (int j = i + 1; j < numbers.length; j++) {
int sum = numbers[i] + numbers[j];
if (sum == targetSum) {
System.out.println(" " + numbers[i] + " + " + numbers[j] + " = " + targetSum);
}
}
}
// All possible pairs (cartesian product)
System.out.println("\nAll pairs (order matters):");
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers.length; j++) {
if (i != j) {
System.out.println(" (" + numbers[i] + ", " + numbers[j] + ")");
}
}
}
}
}
public class Pairs {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40};
int targetSum = 45;
System.out.println("Find pairs that sum to " + targetSum + ":");
// All pairs (including same index)
System.out.println("\nAll pairs with sum " + targetSum + ":");
for (int i = 0; i < numbers.length; i++) {
for (int j = i + 1; j < numbers.length; j++) {
int sum = numbers[i] + numbers[j];
if (sum == targetSum) {
System.out.println(" " + numbers[i] + " + " + numbers[j] + " = " + targetSum);
}
}
}
// All possible pairs (cartesian product)
System.out.println("\nAll pairs (order matters):");
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers.length; j++) {
if (i != j) {
System.out.println(" (" + numbers[i] + ", " + numbers[j] + ")");
}
}
}
}
}
targetSum ← 50
1public class Pairs {2 public static void main(String[] args) {3 int[] numbers = {10, 20, 30, 40}; //@numbers={5, 10, 15, 20, 25}4 int targetSum→ 50 = 50; //@targetSum=30, 455 6 System.out.println("Find pairs that sum to " + targetSum50 + ":");7 8 // All pairs (including same index)9 System.out.println("\nAll pairs with sum " + targetSum50 + ":");10 for (int i = 0; i < numbers.length; i++) {outputFind pairs that sum to 50: All pairs with sum 50:for (int i = 0; i < numbers.length; i++)
pass 1 of 49System.out.println("\nAll pairs with sum " + targetSum + ":");10for (int i0 = 0; i < numbers.length4; i++) {11 for (int j = i + 1; j < numbers.length; j++) { //#?avoid_duplicatesAll 4 passes — pass 1 is the card above pass isumtargetSumnumbers[i]numbers[j]j1 0 50 50 10 40 3 2 1 50 50 20 30 2 3 2 — — — — — 4 3 — — — — — sum ← 30
pass 1 of 610for (int i = 0; i < numbers.length; i++) {11 for (int j1 = i0 + 1; j < numbers.length4; j++) { //#?avoid_duplicates12 int sum→ 30 = numbers[i]10 + numbers[j]20;13 if (sum == targetSum) {All 6 passes — pass 1 is the card above pass jinumbers[i]numbers[j]targetSumsum1 1 0 10 20 — 30 2 2 0 10 30 — 40 3 3 0 10 40 50 50 4 2 1 20 30 50 50 5 3 1 20 40 — 60 6 3 2 30 40 — 70 if (sum == targetSum)
pass 1 of 212int sum = numbers[i] + numbers[j];13if (sum50 == targetSum50) {14 System.out.println(" " + numbers[i]10 + " + " + numbers[j]40 + " = " + targetSum50);15}output 10 + 40 = 50values this step0i3jif (sum == targetSum)
pass 2 of 212int sum = numbers[i] + numbers[j];13if (sum50 == targetSum50) {14 System.out.println(" " + numbers[i]20 + " + " + numbers[j]30 + " = " + targetSum50);15}output 20 + 30 = 50values this step1i2jSystem.out.println(" All pairs (order matters):");
19// All possible pairs (cartesian product)20System.out.println("\nAll pairs (order matters):");21for (int i = 0; i < numbers.length; i++) {output All pairs (order matters):for (int i = 0; i < numbers.length; i++)
pass 1 of 420System.out.println("\nAll pairs (order matters):");21for (int i0 = 0; i < numbers.length4; i++) {22 for (int j = 0; j < numbers.length; j++) {All 4 passes — pass 1 is the card above pass i1 0 2 1 3 2 4 3 for (int j = 0; j < numbers.length; j++)
pass 1 of 1621for (int i = 0; i < numbers.length; i++) {22 for (int j0 = 0; j < numbers.length4; j++) {23 if (i != j) {16 passes — pass 1 is the card above pass j1 0 2 1 3 2 4 3 5 0 6 1 7 2 8 3 9 0 ⋯ 5 more passes ⋯ 15 2 16 3 if (i != j)
pass 1 of 1222for (int j = 0; j < numbers.length; j++) {23 if (i0 != j1) {24 System.out.println(" (" + numbers[i]10 + ", " + numbers[j]20 + ")");25 }output (10, 20)All 12 passes — pass 1 is the card above pass ijnumbers[i]numbers[j]1 0 1 10 20 2 0 2 10 30 3 0 3 10 40 4 1 0 20 10 5 1 2 20 30 6 1 3 20 40 7 2 0 30 10 8 2 1 30 20 9 2 3 30 40 10 3 0 40 10 11 3 1 40 20 12 3 2 40 30
targetSum ← 50
1public class Pairs {2 public static void main(String[] args) {3 int[] numbers = {5, 10, 15, 20, 25};4 int targetSum→ 50 = 50;5 6 System.out.println("Find pairs that sum to " + targetSum50 + ":");7 8 // All pairs (including same index)9 System.out.println("\nAll pairs with sum " + targetSum50 + ":");10 for (int i = 0; i < numbers.length; i++) {outputFind pairs that sum to 50: All pairs with sum 50:for (int i = 0; i < numbers.length; i++)
pass 1 of 59System.out.println("\nAll pairs with sum " + targetSum + ":");10for (int i0 = 0; i < numbers.length5; i++) {11 for (int j = i + 1; j < numbers.length; j++) {All 5 passes — pass 1 is the card above pass i1 0 2 1 3 2 4 3 5 4 sum ← 15
pass 1 of 1010for (int i = 0; i < numbers.length; i++) {11 for (int j1 = i0 + 1; j < numbers.length5; j++) {12 int sum→ 15 = numbers[i]5 + numbers[j]10;13 if (sum == targetSum) {All 10 passes — pass 1 is the card above pass jinumbers[i]numbers[j]sum1 1 0 5 10 15 2 2 0 5 15 20 3 3 0 5 20 25 4 4 0 5 25 30 5 2 1 10 15 25 6 3 1 10 20 30 7 4 1 10 25 35 8 3 2 15 20 35 9 4 2 15 25 40 10 4 3 20 25 45 System.out.println(" All pairs (order matters):");
19// All possible pairs (cartesian product)20System.out.println("\nAll pairs (order matters):");21for (int i = 0; i < numbers.length; i++) {output All pairs (order matters):for (int i = 0; i < numbers.length; i++)
pass 1 of 520System.out.println("\nAll pairs (order matters):");21for (int i0 = 0; i < numbers.length5; i++) {22 for (int j = 0; j < numbers.length; j++) {All 5 passes — pass 1 is the card above pass i1 0 2 1 3 2 4 3 5 4 for (int j = 0; j < numbers.length; j++)
pass 1 of 2521for (int i = 0; i < numbers.length; i++) {22 for (int j0 = 0; j < numbers.length5; j++) {23 if (i != j) {25 passes — pass 1 is the card above pass j1 0 2 1 3 2 4 3 5 4 6 0 7 1 8 2 9 3 ⋯ 14 more passes ⋯ 24 3 25 4 if (i != j)
pass 1 of 2022for (int j = 0; j < numbers.length; j++) {23 if (i0 != j1) {24 System.out.println(" (" + numbers[i]5 + ", " + numbers[j]10 + ")");25 }output (5, 10)20 passes — pass 1 is the card above pass ijnumbers[i]numbers[j]1 0 1 5 10 2 0 2 5 15 3 0 3 5 20 4 0 4 5 25 5 1 0 10 5 6 1 2 10 15 7 1 3 10 20 8 1 4 10 25 9 2 0 15 5 ⋯ 9 more passes ⋯ 19 4 2 25 15 20 4 3 25 20
targetSum ← 30
1public class Pairs {2 public static void main(String[] args) {3 int[] numbers = {10, 20, 30, 40};4 int targetSum→ 30 = 30;5 6 System.out.println("Find pairs that sum to " + targetSum30 + ":");7 8 // All pairs (including same index)9 System.out.println("\nAll pairs with sum " + targetSum30 + ":");10 for (int i = 0; i < numbers.length; i++) {outputFind pairs that sum to 30: All pairs with sum 30:for (int i = 0; i < numbers.length; i++)
pass 1 of 49System.out.println("\nAll pairs with sum " + targetSum + ":");10for (int i0 = 0; i < numbers.length4; i++) {11 for (int j = i + 1; j < numbers.length; j++) {All 4 passes — pass 1 is the card above pass isumtargetSumnumbers[i]numbers[j]j1 0 30 30 10 20 1 2 1 — — — — — 3 2 — — — — — 4 3 — — — — — sum ← 30
pass 1 of 610for (int i = 0; i < numbers.length; i++) {11 for (int j1 = i0 + 1; j < numbers.length4; j++) {12 int sum→ 30 = numbers[i]10 + numbers[j]20;13 if (sum == targetSum) {All 6 passes — pass 1 is the card above pass jinumbers[i]numbers[j]targetSumsum1 1 0 10 20 30 30 2 2 0 10 30 — 40 3 3 0 10 40 — 50 4 2 1 20 30 — 50 5 3 1 20 40 — 60 6 3 2 30 40 — 70 if (sum == targetSum)
12int sum = numbers[i] + numbers[j];13if (sum30 == targetSum30) {14 System.out.println(" " + numbers[i]10 + " + " + numbers[j]20 + " = " + targetSum30);15}output 10 + 20 = 30values this step0i1jSystem.out.println(" All pairs (order matters):");
19// All possible pairs (cartesian product)20System.out.println("\nAll pairs (order matters):");21for (int i = 0; i < numbers.length; i++) {output All pairs (order matters):for (int i = 0; i < numbers.length; i++)
pass 1 of 420System.out.println("\nAll pairs (order matters):");21for (int i0 = 0; i < numbers.length4; i++) {22 for (int j = 0; j < numbers.length; j++) {All 4 passes — pass 1 is the card above pass i1 0 2 1 3 2 4 3 for (int j = 0; j < numbers.length; j++)
pass 1 of 1621for (int i = 0; i < numbers.length; i++) {22 for (int j0 = 0; j < numbers.length4; j++) {23 if (i != j) {16 passes — pass 1 is the card above pass j1 0 2 1 3 2 4 3 5 0 6 1 7 2 8 3 9 0 ⋯ 5 more passes ⋯ 15 2 16 3 if (i != j)
pass 1 of 1222for (int j = 0; j < numbers.length; j++) {23 if (i0 != j1) {24 System.out.println(" (" + numbers[i]10 + ", " + numbers[j]20 + ")");25 }output (10, 20)All 12 passes — pass 1 is the card above pass ijnumbers[i]numbers[j]1 0 1 10 20 2 0 2 10 30 3 0 3 10 40 4 1 0 20 10 5 1 2 20 30 6 1 3 20 40 7 2 0 30 10 8 2 1 30 20 9 2 3 30 40 10 3 0 40 10 11 3 1 40 20 12 3 2 40 30
targetSum ← 45
1public class Pairs {2 public static void main(String[] args) {3 int[] numbers = {10, 20, 30, 40};4 int targetSum→ 45 = 45;5 6 System.out.println("Find pairs that sum to " + targetSum45 + ":");7 8 // All pairs (including same index)9 System.out.println("\nAll pairs with sum " + targetSum45 + ":");10 for (int i = 0; i < numbers.length; i++) {outputFind pairs that sum to 45: All pairs with sum 45:for (int i = 0; i < numbers.length; i++)
pass 1 of 49System.out.println("\nAll pairs with sum " + targetSum + ":");10for (int i0 = 0; i < numbers.length4; i++) {11 for (int j = i + 1; j < numbers.length; j++) {All 4 passes — pass 1 is the card above pass i1 0 2 1 3 2 4 3 sum ← 30
pass 1 of 610for (int i = 0; i < numbers.length; i++) {11 for (int j1 = i0 + 1; j < numbers.length4; j++) {12 int sum→ 30 = numbers[i]10 + numbers[j]20;13 if (sum == targetSum) {All 6 passes — pass 1 is the card above pass jinumbers[i]numbers[j]sum1 1 0 10 20 30 2 2 0 10 30 40 3 3 0 10 40 50 4 2 1 20 30 50 5 3 1 20 40 60 6 3 2 30 40 70 System.out.println(" All pairs (order matters):");
19// All possible pairs (cartesian product)20System.out.println("\nAll pairs (order matters):");21for (int i = 0; i < numbers.length; i++) {output All pairs (order matters):for (int i = 0; i < numbers.length; i++)
pass 1 of 420System.out.println("\nAll pairs (order matters):");21for (int i0 = 0; i < numbers.length4; i++) {22 for (int j = 0; j < numbers.length; j++) {All 4 passes — pass 1 is the card above pass i1 0 2 1 3 2 4 3 for (int j = 0; j < numbers.length; j++)
pass 1 of 1621for (int i = 0; i < numbers.length; i++) {22 for (int j0 = 0; j < numbers.length4; j++) {23 if (i != j) {16 passes — pass 1 is the card above pass j1 0 2 1 3 2 4 3 5 0 6 1 7 2 8 3 9 0 ⋯ 5 more passes ⋯ 15 2 16 3 if (i != j)
pass 1 of 1222for (int j = 0; j < numbers.length; j++) {23 if (i0 != j1) {24 System.out.println(" (" + numbers[i]10 + ", " + numbers[j]20 + ")");25 }output (10, 20)All 12 passes — pass 1 is the card above pass ijnumbers[i]numbers[j]1 0 1 10 20 2 0 2 10 30 3 0 3 10 40 4 1 0 20 10 5 1 2 20 30 6 1 3 20 40 7 2 0 30 10 8 2 1 30 20 9 2 3 30 40 10 3 0 40 10 11 3 1 40 20 12 3 2 40 30
Comparing every element with every other element requires O(n²) nested loops.
Exercise: Diamond.java
Create a diamond shape pattern with careful loop control