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

Java Patterns CheatSheet

The document provides a cheat sheet for Java pattern printing, detailing various patterns such as right-angled triangles, inverted triangles, pyramids, diamonds, number triangles, Floyd's triangle, and hollow rectangles. Each pattern includes the output format, logic explanation, and corresponding Java code. This serves as a quick reference for programmers to implement these common patterns in Java.

Uploaded by

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

Java Patterns CheatSheet

The document provides a cheat sheet for Java pattern printing, detailing various patterns such as right-angled triangles, inverted triangles, pyramids, diamonds, number triangles, Floyd's triangle, and hollow rectangles. Each pattern includes the output format, logic explanation, and corresponding Java code. This serves as a quick reference for programmers to implement these common patterns in Java.

Uploaded by

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

Java Pattern Printing Cheat Sheet

Pattern: Right-Angled Triangle


Output:
*
**
***
****
Logic: Stars in each row = row number
Code:
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
[Link]("*");
}
[Link]();
}

Pattern: Inverted Triangle


Output:
****
***
**
*
Logic: Stars in each row = n - row + 1
Code:
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
[Link]("*");
}
[Link]();
}

Pattern: Pyramid
Output:
*
***
*****
*******
Logic: Spaces = n - row, Stars = 2*row - 1
Code:
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) {
[Link](" ");
}
for (int j = 1; j <= 2*i - 1; j++) {
[Link]("*");
}
[Link]();
}

Pattern: Diamond
Output:
*
***
*****
*******
*****
***
*
Logic: Upper pyramid + Inverted pyramid
Code:
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) [Link](" ");
for (int j = 1; j <= 2*i - 1; j++) [Link]("*");
[Link]();
}
for (int i = n-1; i >= 1; i--) {
for (int s = 1; s <= n - i; s++) [Link](" ");
for (int j = 1; j <= 2*i - 1; j++) [Link]("*");
[Link]();
}

Pattern: Number Triangle


Output:
1
12
123
1234
Logic: Print column index j
Code:
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
[Link](j);
}
[Link]();
}

Pattern: Floyd’s Triangle


Output:
1
2 3
4 5 6
7 8 9 10
Logic: Use counter variable that increments every print
Code:
int count = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
[Link](count + " ");
count++;
}
[Link]();
}

Pattern: Hollow Rectangle


Output:
*****
* *
* *
*****
Logic: If border (i==1, i==n, j==1, j==m) print *, else space
Code:
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (i == 1 || i == n || j == 1 || j == m) {
[Link]("*");
} else {
[Link](" ");
}
}
[Link]();
}

You might also like