25 Pattern Problems with Java Solutions
1. Square Pattern
Input: n = 4
Output:
****
****
****
****
Code:
public class Solution {
public static void squarePattern(int n) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
[Link]("*");
}
[Link]();
}
}
}
2. Right-Angle Triangle
Input: n = 4
Output:
*
**
***
****
Code:
public class Solution {
public static void rightTriangle(int n) {
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
[Link]("*");
}
[Link]();
}
}
}
3. Number Crown Pattern
Input: n = 3
Output:
1 1
12 21
123321
Code:
public class Solution {
public static void numberCrown(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
[Link](j + " ");
}
for (int s = 1; s <= 2 * (n - i); s++) {
[Link](" ");
}
for (int j = i; j >= 1; j--) {
[Link](j + " ");
}
[Link]();
}
}
}