Java Star Triangle Patterns
ICSE Class 10 — Nested Loop Programs using Stars | All 4 Right-Angle Triangle Orientations
Pattern 1 — Right Angle at Bottom-Left
(Increasing rows, left-aligned)
Expected Output (n = 5):
*
**
***
****
*****
Java Code:
public class Pattern1 {
public static void main(String[] args) {
int n = 5; // Number of rows
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
[Link]("*");
}
[Link]();
}
}
}
Pattern 2 — Right Angle at Bottom-Right
(Increasing rows, right-aligned)
Expected Output (n = 5):
*
**
***
****
*****
Java Code:
public class Pattern2 {
public static void main(String[] args) {
int n = 5; // Number of rows
for (int i = 1; i <= n; i++) {
// Print leading spaces
for (int s = 1; s <= n - i; s++) {
[Link](" ");
}
// Print stars
for (int j = 1; j <= i; j++) {
[Link]("*");
}
[Link]();
}
}
}
Pattern 3 — Right Angle at Top-Left
(Decreasing rows, left-aligned)
Expected Output (n = 5):
*****
****
***
**
*
Java Code:
public class Pattern3 {
public static void main(String[] args) {
int n = 5; // Number of rows
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
[Link]("*");
}
[Link]();
}
}
}
Pattern 4 — Right Angle at Top-Right
(Decreasing rows, right-aligned)
Expected Output (n = 5):
*****
****
***
**
*
Java Code:
public class Pattern4 {
public static void main(String[] args) {
int n = 5; // Number of rows
for (int i = n; i >= 1; i--) {
// Print leading spaces
for (int s = 1; s <= n - i; s++) {
[Link](" ");
}
// Print stars
for (int j = 1; j <= i; j++) {
[Link]("*");
}
[Link]();
}
}
}
Notes
• Variable n controls the number of rows. Change n to get a bigger or smaller triangle.
• Outer loop → controls the row number (i).
• Inner loop 1 → prints leading spaces (used in Patterns 2 and 4 for right-alignment).
• Inner loop 2 → prints the star (*) characters.
• [Link]() moves to the next line after each row is printed.