0% found this document useful (0 votes)
7 views3 pages

Java Star Triangles ICSE

The document provides Java code examples for creating four different right-angle triangle patterns using nested loops. Each pattern demonstrates varying alignments and orientations, including bottom-left, bottom-right, top-left, and top-right configurations. Additionally, it includes notes on how to modify the number of rows and the structure of the loops used in the code.

Uploaded by

manas.dhruvdev
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Java Star Triangles ICSE

The document provides Java code examples for creating four different right-angle triangle patterns using nested loops. Each pattern demonstrates varying alignments and orientations, including bottom-left, bottom-right, top-left, and top-right configurations. Additionally, it includes notes on how to modify the number of rows and the structure of the loops used in the code.

Uploaded by

manas.dhruvdev
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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.

You might also like