Master Pattern Printing Tutorial: 118 Pattern Problems & Solutions
Master Java Pattern Printing Tutorial
Welcome to the complete Java Pattern Printing tutorial for ! This guide contains 118 pattern problems covering Stars, Numbers, Letters, and Mixed Advanced Patterns. Each solution includes the input, visual output, and standard Java solution code using the IO helper class.
Problems : https://drive.google.com/file/d/1AQeYF2oHRRjCy2V92wlSXxpsta4uoklA/view?usp=sharing
Section 1: Star Patterns
1.1. Solid Square of Stars
Input: 5
Output:
*****
*****
*****
*****
*****class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
IO.print("*");
}
IO.println();
}
}
}1.2. Right Triangle of Stars
Input: 5
Output:
*
**
***
****
*****class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
IO.print("*");
}
IO.println();
}
}
}1.3. Right Triangle with Leading Spaces of Stars
Input: 5
Output:
*
**
***
****
*****class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = 1; k <= i; k++) {
IO.print("*");
}
IO.println();
}
}
}1.4. Inverted Right Triangle of Stars
Input: 5
Output:
*****
****
***
**
*class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i + 1; j++) {
IO.print("*");
}
IO.println();
}
}
}1.5. Inverted Right Triangle with Leading Spaces of Stars
Input: 5
Output:
*****
****
***
**
*class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
IO.print(" ");
}
for (int k = 1; k <= n - i + 1; k++) {
IO.print("*");
}
IO.println();
}
}
}1.6. Pyramid of Stars
Input: 5
Output:
*
***
*****
*******
*********class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
IO.print("*");
}
IO.println();
}
}
}1.7. Inverted Pyramid of Stars
Input: 5
Output:
*********
*******
*****
***
*class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
IO.print(" ");
}
for (int k = 1; k <= 2 * (n - i) + 1; k++) {
IO.print("*");
}
IO.println();
}
}
}1.8. Right Pascal Triangle
Input: 5
Output:
*
**
***
****
*****
****
***
**
*class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = 1; k <= i; k++) {
IO.print("*");
}
IO.println();
}
for (int i = 1; i <= n - 1; i++) {
for (int j = 1; j <= i; j++) {
IO.print(" ");
}
for (int k = 1; k <= n - i; k++) {
IO.print("*");
}
IO.println();
}
}
}1.9. Left Pascal Triangle
Input: 5
Output:
*
**
***
****
*****
****
***
**
*class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
IO.print("*");
}
IO.println();
}
for (int i = 1; i <= n - 1; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print("*");
}
IO.println();
}
}
}1.10. Right Half Hour Glass of Stars
Input: 5
Output:
*****
****
***
**
*
**
***
****
*****class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i + 1; j++) {
IO.print("*");
}
IO.println();
}
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i; j++) {
IO.print("*");
}
IO.println();
}
}
}1.11. Left Half Hour Glass of Stars
Input: 5
Output:
*****
****
***
**
*
**
***
****
*****class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
IO.print(" ");
}
for (int k = 1; k <= n - i + 1; k++) {
IO.print("*");
}
IO.println();
}
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = 1; k <= i; k++) {
IO.print("*");
}
IO.println();
}
}
}1.12. Diamond of Stars
Input: 5
Output:
*
***
*****
*******
*********
*******
*****
***
*class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
IO.print("*");
}
IO.println();
}
for (int i = 1; i <= n - 1; i++) {
for (int j = 1; j <= i; j++) {
IO.print(" ");
}
for (int k = 1; k <= 2 * (n - i) - 1; k++) {
IO.print("*");
}
IO.println();
}
}
}1.13. Hourglass of Stars
Input: 5
Output:
*********
*******
*****
***
*
***
*****
*******
*********class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
IO.print(" ");
}
for (int k = 1; k <= 2 * (n - i) + 1; k++) {
IO.print("*");
}
IO.println();
}
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
IO.print("*");
}
IO.println();
}
}
}1.14. Twin Peaks of Stars
Input: 5
Output:
* *
** **
*** ***
**** ****
**********class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
IO.print("*");
}
for (int k = 1; k <= 2 * (n - i); k++) {
IO.print(" ");
}
for (int l = 1; l <= i; l++) {
IO.print("*");
}
IO.println();
}
}
}1.15. Inverted Twin Peaks of Stars
Input: 5
Output:
**********
**** ****
*** ***
** **
* *class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i + 1; j++) {
IO.print("*");
}
for (int k = 1; k <= 2 * (i - 1); k++) {
IO.print(" ");
}
for (int l = 1; l <= n - i + 1; l++) {
IO.print("*");
}
IO.println();
}
}
}1.16. Butterfly of Stars
Input: 5
Output:
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
IO.print("*");
}
for (int k = 1; k <= 2 * (n - i); k++) {
IO.print(" ");
}
for (int l = 1; l <= i; l++) {
IO.print("*");
}
IO.println();
}
for (int i = 1; i <= n - 1; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print("*");
}
for (int k = 1; k <= 2 * i; k++) {
IO.print(" ");
}
for (int l = 1; l <= n - i; l++) {
IO.print("*");
}
IO.println();
}
}
}1.17. Hollow Diamond of Stars
Input: 5
Output:
**********
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
**********class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i + 1; j++) {
IO.print("*");
}
for (int k = 1; k <= 2 * (i - 1); k++) {
IO.print(" ");
}
for (int l = 1; l <= n - i + 1; l++) {
IO.print("*");
}
IO.println();
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
IO.print("*");
}
for (int k = 1; k <= 2 * (n - i); k++) {
IO.print(" ");
}
for (int l = 1; l <= i; l++) {
IO.print("*");
}
IO.println();
}
}
}1.18. Hollow Square of Stars
Input: 5
Output:
*****
* *
* *
* *
*****class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == 1 || i == n || j == 1 || j == n) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
}
}1.19. Hollow Right Triangle of Stars
Input: 5
Output:
*
**
* *
* *
*****class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || j == i || i == n) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
}
}1.20. Hollow Right Triangle with Leading Spaces of Stars
Input: 5
Output:
*
**
* *
* *
*****class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = 1; k <= i; k++) {
if (k == 1 || k == i || i == n) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
}
}1.21. Hollow Inverted Right Triangle of Stars
Input: 5
Output:
*****
* *
* *
**
*class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i + 1; j++) {
if (i == 1 || j == 1 || j == n - i + 1) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
}
}1.22. Hollow Inverted Right Triangle with Leading Spaces of Stars
Input: 5
Output:
*****
* *
* *
**
*class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
IO.print(" ");
}
for (int k = 1; k <= n - i + 1; k++) {
if (i == 1 || k == 1 || k == n - i + 1) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
}
}1.23. Hollow Pyramid of Stars
Input: 5
Output:
*
* *
* *
* *
*********class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
if (k == 1 || k == 2 * i - 1 || i == n) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
}
}1.24. Hollow Inverted Pyramid of Stars
Input: 5
Output:
*********
* *
* *
* *
*class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
IO.print(" ");
}
for (int k = 1; k <= 2 * (n - i) + 1; k++) {
if (i == 1 || k == 1 || k == 2 * (n - i) + 1) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
}
}1.25. Hollow Right Pascal Triangle
Input: 5
Output:
*
**
* *
* *
*****
* *
* *
**
*class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = 1; k <= i; k++) {
if (k == 1 || k == i) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
for (int i = 1; i <= n - 1; i++) {
for (int j = 1; j <= i; j++) {
IO.print(" ");
}
for (int k = 1; k <= n - i; k++) {
if (k == 1 || k == n - i) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
}
}1.26. Hollow Left Pascal Triangle
Input: 5
Output:
*
**
* *
* *
* *
* *
* *
**
*class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || j == i) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
for (int i = 1; i <= n - 1; i++) {
for (int j = 1; j <= n - i; j++) {
if (j == 1 || j == n - i) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
}
}1.27. Hollow Right Half Hour Glass of Stars
Input: 5
Output:
*****
* *
* *
**
*
**
* *
* *
*****class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i + 1; j++) {
if (i == 1 || j == 1 || j == n - i + 1) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (i == n || j == 1 || j == i) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
}
}1.28. Hollow Left Half Hour Glass of Stars
Input: 5
Output:
*****
* *
* *
**
*
* *
* *
*****class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
IO.print(" ");
}
for (int k = 1; k <= n - i + 1; k++) {
if (i == 1 || k == 1 || k == n - i + 1) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = 1; k <= i; k++) {
if (i == n || k == 1 || k == i) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
}
}1.29. Hollow Diamond of Stars
Input: 5
Output:
*
* *
* *
* *
* *
* *
* *
* *
*class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
if (k == 1 || k == 2 * i - 1) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
for (int i = 1; i <= n - 1; i++) {
for (int j = 1; j <= i; j++) {
IO.print(" ");
}
for (int k = 1; k <= 2 * (n - i) - 1; k++) {
if (k == 1 || k == 2 * (n - i) - 1) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
}
}1.30. Hollow Hourglass of Stars
Input: 5
Output:
*********
* *
* *
* *
*
* *
* *
* *
*********class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
IO.print(" ");
}
for (int k = 1; k <= 2 * (n - i) + 1; k++) {
if (i == 1 || k == 1 || k == 2 * (n - i) + 1) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
if (i == n || k == 1 || k == 2 * i - 1) {
IO.print("*");
} else {
IO.print(" ");
}
}
IO.println();
}
}
}Section 2: Number Patterns
2.1. Row Numbers Square
Input: 5
Output:
11111
22222
33333
44444
55555class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
IO.print(i);
}
IO.println();
}
}
}2.2. Inverted Row Numbers Square
Input: 5
Output:
55555
44444
33333
22222
11111class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
IO.print(n - i + 1);
}
IO.println();
}
}
}2.3. Column Numbers Square
Input: 5
Output:
12345
12345
12345
12345
12345class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
IO.print(j);
}
IO.println();
}
}
}2.4. Inverted Column Numbers Square
Input: 5
Output:
54321
54321
54321
54321
54321class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = n; j >= 1; j--) {
IO.print(j);
}
IO.println();
}
}
}2.5. Checkerboard Pattern
Input: 5
Output:
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if ((i + j) % 2 == 0) {
IO.print("1");
} else {
IO.print("0");
}
if (j < n) IO.print(" ");
}
IO.println();
}
}
}2.6. Hollow Square of Numbers
Input: 5
Output:
11111
10001
10001
10001
11111class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == 1 || i == n || j == 1 || j == n) {
IO.print("1");
} else {
IO.print("0");
}
}
IO.println();
}
}
}2.7. Column Numbers Square with Leading Spaces
Input: 5
Output:
12345
12345
12345
12345
12345class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
int spaces = (i == 1) ? 0 : n - i;
for (int j = 1; j <= spaces; j++) {
IO.print(" ");
}
for (int j = 1; j <= n; j++) {
IO.print(j);
}
IO.println();
}
}
}2.8. Inverted Column Numbers Square with Leading Spaces
Input: 5
Output:
54321
54321
54321
54321
54321class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
IO.print(" ");
}
for (int j = n; j >= 1; j--) {
IO.print(j);
}
IO.println();
}
}
}2.9. Staircase Numbers
Input: 5
Output:
11
22
33
44
55class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
IO.print(" ");
}
IO.print(i + "" + i);
IO.println();
}
}
}2.10. Double Column Numbers Square
Input: 5
Output:
1122334455
1122334455
1122334455
1122334455
1122334455class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
IO.print(j + "" + j);
}
IO.println();
}
}
}2.11. Right Triangle Of Column Numbers
Input: 5
Output:
1
12
123
1234
12345class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
IO.print(j);
}
IO.println();
}
}
}2.12. Right Triangle Of Inverted Column Numbers
Input: 5
Output:
5
54
543
5432
54321class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = n; j >= n - i + 1; j--) {
IO.print(j);
}
IO.println();
}
}
}2.13. Inverted Right Triangle with Leading Spaces
Input: 5
Output:
1
12
123
1234
12345class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = 1; k <= i; k++) {
IO.print(k);
}
IO.println();
}
}
}2.14. Inverted Right Triangle with Leading Spaces Reversed
Input: 5
Output:
5
54
543
5432
54321class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = n; k >= n - i + 1; k--) {
IO.print(k);
}
IO.println();
}
}
}2.15. Inverted Right Triangle with Leading Spaces of Column Numbers
Input: 5
Output:
1
21
321
4321
54321class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = i; k >= 1; k--) {
IO.print(k);
}
IO.println();
}
}
}2.16. Pyramid Numbers
Input: 5
Output:
1
121
12321
1234321
123454321class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = 1; k <= i; k++) {
IO.print(k);
}
for (int l = i - 1; l >= 1; l--) {
IO.print(l);
}
IO.println();
}
}
}2.17. Inverted Pyramid Numbers
Input: 5
Output:
5
545
54345
5432345
543212345class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = n; k >= n - i + 1; k--) {
IO.print(k);
}
for (int l = n - i + 2; l <= n; l++) {
IO.print(l);
}
IO.println();
}
}
}2.18. Odd Numbers Triangle
Input: 5
Output:
1
3 5
7 9 11
13 15 17 19
21 23 25 27 29class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
int num = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
IO.print(num);
if (j < i) IO.print(" ");
num += 2;
}
IO.println();
}
}
}2.19. Even Numbers Triangle
Input: 5
Output:
2
4 6
8 10 12
14 16 18 20
22 24 26 28 30class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
int num = 2;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
IO.print(num);
if (j < i) IO.print(" ");
num += 2;
}
IO.println();
}
}
}2.20. Inverted Pyramid of Palindrome Numbers
Input: 5
Output:
123454321
1234321
12321
121
1class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
IO.print(" ");
}
for (int k = 1; k <= n - i + 1; k++) {
IO.print(k);
}
for (int l = n - i; l >= 1; l--) {
IO.print(l);
}
IO.println();
}
}
}2.21. Inverted Pyramid of Reverse Palindrome Numbers
Input: 5
Output:
543212345
5432345
54345
545
5class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
IO.print(" ");
}
for (int k = n; k >= i; k--) {
IO.print(k);
}
for (int l = i + 1; l <= n; l++) {
IO.print(l);
}
IO.println();
}
}
}2.22. Palindrome Diamond
Input: 5
Output:
1
121
12321
1234321
123454321
1234321
12321
121
1class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = 1; k <= i; k++) {
IO.print(k);
}
for (int l = i - 1; l >= 1; l--) {
IO.print(l);
}
IO.println();
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = 1; k <= i; k++) {
IO.print(k);
}
for (int l = i - 1; l >= 1; l--) {
IO.print(l);
}
IO.println();
}
}
}2.23. Inverted Palindrome Diamond
Input: 5
Output:
5
545
54345
5432345
543212345
5432345
54345
545
5class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = n; k >= n - i + 1; k--) {
IO.print(k);
}
for (int l = n - i + 2; l <= n; l++) {
IO.print(l);
}
IO.println();
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
IO.print(" ");
}
for (int k = n; k >= n - i + 1; k--) {
IO.print(k);
}
for (int l = n - i + 2; l <= n; l++) {
IO.print(l);
}
IO.println();
}
}
}2.24. Twin Peaks of Numbers
Input: 5
Output:
5 5
54 45
543 345
5432 2345
543212345class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = n; j >= n - i + 1; j--) {
IO.print(j);
}
for (int k = 1; k <= 2 * (n - i) - 1; k++) {
IO.print(" ");
}
int start = (i == n) ? n - i + 2 : n - i + 1;
for (int l = start; l <= n; l++) {
IO.print(l);
}
IO.println();
}
}
}2.25. Inverted Twin Peaks of Numbers
Input: 5
Output:
543212345
5432 2345
543 345
54 45
5 5class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = n; j >= i; j--) {
IO.print(j);
}
for (int k = 1; k <= 2 * (i - 1) - 1; k++) {
IO.print(" ");
}
int start = (i == 1) ? i + 1 : i;
for (int l = start; l <= n; l++) {
IO.print(l);
}
IO.println();
}
}
}2.26. Alternating Rows in Square of Numbers
Input: 5
Output:
54321
12345
54321
12345
54321class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
if (i % 2 != 0) {
for (int j = n; j >= 1; j--) IO.print(j);
} else {
for (int j = 1; j <= n; j++) IO.print(j);
}
IO.println();
}
}
}2.27. Alternating Columns in Square of Numbers
Input: 5
Output:
15151
24242
33333
42424
51515class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j % 2 != 0) {
IO.print(i);
} else {
IO.print(n - i + 1);
}
}
IO.println();
}
}
}2.28. Matrix Numbers
Input: 5
Output:
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
41 42 43 44 45
51 52 53 54 55class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
IO.print(i + "" + j);
if (j < n) IO.print(" ");
}
IO.println();
}
}
}2.29. Continuous Matrix Numbers
Input: 5
Output:
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
31 32 33 34 35
36 37 38 39 40class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
int num = 11;
for (int i = 1; i <= n + 1; i++) {
for (int j = 1; j <= n; j++) {
IO.print(num);
if (j < n) IO.print(" ");
num++;
}
IO.println();
}
}
}2.30. Rectangle of Column Numbers
Input: 4 5
Output:
12345
12345
12345
12345class pattern {
public static void main (String[] args){
String[] parts = IO.readln().split(" ");
int r = Integer.parseInt(parts[0]);
int c = Integer.parseInt(parts[1]);
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
IO.print(j);
}
IO.println();
}
}
}2.31. Rectangle of Index Numbers
Input: 4 5
Output:
11 12 13 14 15
21 22 23 24 25
31 32 33 34 35
41 42 43 44 45class pattern {
public static void main (String[] args){
String[] parts = IO.readln().split(" ");
int r = Integer.parseInt(parts[0]);
int c = Integer.parseInt(parts[1]);
for (int i = 1; i <= r; i++) {
for (int j = 1; j <= c; j++) {
IO.print(i + "" + j);
if (j < c) IO.print(" ");
}
IO.println();
}
}
}2.32. Cyclic Rectangle of Numbers
Input: 4 10
Output:
1234567891
2345678912
3456789123
4567891234class pattern {
public static void main (String[] args){
String[] parts = IO.readln().split(" ");
int r = Integer.parseInt(parts[0]);
int c = Integer.parseInt(parts[1]);
for (int i = 1; i <= r; i++) {
int start = i;
for (int j = 0; j < c; j++) {
int num = (start - 1 + j) % 9 + 1;
IO.print(num);
}
IO.println();
}
}
}2.33. Hollow Square with Row Numbers
Input: 5
Output:
12345
1 5
1 5
1 5
12345class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == 1 || i == n || j == 1 || j == n) {
IO.print(j);
} else {
IO.print(" ");
}
}
IO.println();
}
}
}2.34. The Concentric Number Square
Input: 5
Output:
5555555
5444445
5433345
5432345
5433345
5444445
5555555class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
int size = 2 * n - 3;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
int minDist = Math.min(Math.min(i, j), Math.min(size - 1 - i, size - 1 - j));
IO.print(n - minDist);
}
IO.println();
}
}
}2.35. Hollow Pyramid of Numbers
Input: 5
Output:
1
2 2
3 3
4 4
543212345class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
if (i < n) {
for (int j = 1; j <= n - i; j++) IO.print(" ");
if (i == 1) {
IO.print(1);
} else {
IO.print(i);
for (int k = 1; k <= 2 * i - 3; k++) IO.print(" ");
IO.print(i);
}
} else {
for (int j = n; j >= 1; j--) IO.print(j);
for (int j = 2; j <= n; j++) IO.print(j);
}
IO.println();
}
}
}2.36. Hollow Diamond of Numbers
Input: 5
Output:
1
2 2
3 3
4 4
5 5
4 4
3 3
2 2
1class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) IO.print(" ");
if (i == 1) {
IO.print(1);
} else {
IO.print(i);
for (int k = 1; k <= 2 * i - 3; k++) IO.print(" ");
IO.print(i);
}
IO.println();
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) IO.print(" ");
if (i == 1) {
IO.print(1);
} else {
IO.print(i);
for (int k = 1; k <= 2 * i - 3; k++) IO.print(" ");
IO.print(i);
}
IO.println();
}
}
}2.37. X Shape Numbers
Input: 5
Output:
1 1
2 2
3
4 4
5 5class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j == i || j == n - i + 1) {
IO.print(i);
} else {
IO.print(" ");
}
}
IO.println();
}
}
}Section 3: Letter Patterns
3.1. Solid Square of Letters
Input: 5
Output:
AAAAA
BBBBB
CCCCC
DDDDD
EEEEEclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
char ch = (char) ('A' + i);
for (int j = 0; j < n; j++) {
IO.print(ch);
}
IO.println();
}
}
}3.2. Rectangle of Letters
Input: 4 3
Output:
AAA
BBB
CCC
DDDclass pattern {
public static void main (String[] args){
String[] parts = IO.readln().split(" ");
int r = Integer.parseInt(parts[0]);
int c = Integer.parseInt(parts[1]);
for (int i = 0; i < r; i++) {
char ch = (char) ('A' + i);
for (int j = 0; j < c; j++) {
IO.print(ch);
}
IO.println();
}
}
}3.3. Row Letters Square
Input: 5
Output:
ABCDE
ABCDE
ABCDE
ABCDE
ABCDEclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
IO.print((char) ('A' + j));
}
IO.println();
}
}
}3.4. Inverted Row Letters Square
Input: 5
Output:
EDCBA
EDCBA
EDCBA
EDCBA
EDCBAclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = n - 1; j >= 0; j--) {
IO.print((char) ('A' + j));
}
IO.println();
}
}
}3.5. Alternating Rows of Letters
Input: 5
Output:
ABCDE
EDCBA
ABCDE
EDCBA
ABCDEclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
for (int j = 0; j < n; j++) IO.print((char) ('A' + j));
} else {
for (int j = n - 1; j >= 0; j--) IO.print((char) ('A' + j));
}
IO.println();
}
}
}3.6. Alternating Columns of Letters
Input: 5
Output:
AEAEA
BDBDB
CCCCC
DBDBD
EAEAEclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j % 2 == 0) {
IO.print((char) ('A' + i));
} else {
IO.print((char) ('A' + n - 1 - i));
}
}
IO.println();
}
}
}3.7. Inverted Row Letters Z
Input: 5
Output:
ZYXWV
ZYXWV
ZYXWV
ZYXWV
ZYXWVclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
IO.print((char) ('Z' - j));
}
IO.println();
}
}
}3.8. Alternating Rows of Letters Z
Input: 5
Output:
ABCDE
ZYXWV
ABCDE
ZYXWV
ABCDEclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
for (int j = 0; j < n; j++) IO.print((char) ('A' + j));
} else {
for (int j = 0; j < n; j++) IO.print((char) ('Z' - j));
}
IO.println();
}
}
}3.9. Matrix Letters
Input: 5
Output:
AA AB AC AD AE
BA BB BC BD BE
CA CB CC CD CE
DA DB DC DD DE
EA EB EC ED EEclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
IO.print((char)('A' + i) + "" + (char)('A' + j));
if (j < n - 1) IO.print(" ");
}
IO.println();
}
}
}3.10. Alternating Matrix Letters
Input: 5
Output:
AZ BY CX DW EV
AZ BY CX DW EV
AZ BY CX DW EV
AZ BY CX DW EV
AZ BY CX DW EVclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
IO.print((char)('A' + j) + "" + (char)('Z' - j));
if (j < n - 1) IO.print(" ");
}
IO.println();
}
}
}3.11. Alternating Matrix Letters Vertical
Input: 5
Output:
AZ AZ AZ AZ AZ
BY BY BY BY BY
CX CX CX CX CX
DW DW DW DW DW
EV EV EV EV EVclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
IO.print((char)('A' + i) + "" + (char)('Z' - i));
if (j < n - 1) IO.print(" ");
}
IO.println();
}
}
}3.12. Right Square of Row Letters with Leading Spaces
Input: 5
Output:
ABCDE
ABCDE
ABCDE
ABCDE
ABCDEclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
int spaces = (i == 0) ? 0 : n - 1 - i;
for (int j = 0; j < spaces; j++) IO.print(" ");
for (int j = 0; j < n; j++) IO.print((char)('A' + j));
IO.println();
}
}
}3.13. Inverted Right Square of Row Letters with Leading Spaces
Input: 5
Output:
EDCBA
EDCBA
EDCBA
EDCBA
EDCBAclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) IO.print(" ");
for (int j = n - 1; j >= 0; j--) IO.print((char)('A' + j));
IO.println();
}
}
}3.14. Rectangle of Row Letters
Input: 4 5
Output:
ABCDE
ABCDE
ABCDE
ABCDEclass pattern {
public static void main (String[] args){
String[] parts = IO.readln().split(" ");
int r = Integer.parseInt(parts[0]);
int c = Integer.parseInt(parts[1]);
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) IO.print((char)('A' + j));
IO.println();
}
}
}3.15. Right Triangle of Letters
Input: 5
Output:
A
AB
ABC
ABCD
ABCDEclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) IO.print((char)('A' + j));
IO.println();
}
}
}3.16. Right Triangle of Letters with Leading Spaces
Input: 5
Output:
A
AB
ABC
ABCD
ABCDEclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1 - i; j++) IO.print(" ");
for (int j = 0; j <= i; j++) IO.print((char)('A' + j));
IO.println();
}
}
}3.17. Inverted Right Triangle of Letters
Input: 5
Output:
ABCDE
ABCD
ABC
AB
Aclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) IO.print((char)('A' + j));
IO.println();
}
}
}3.18. Inverted Right Triangle with Leading Spaces
Input: 5
Output:
ABCDE
ABCD
ABC
AB
Aclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) IO.print(" ");
for (int j = 0; j < n - i; j++) IO.print((char)('A' + j));
IO.println();
}
}
}3.19. Inverted Right Triangle with Leading Spaces E
Input: 5
Output:
EDCBA
DCBA
CBA
BA
Aclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) IO.print(" ");
for (int j = n - 1 - i; j >= 0; j--) IO.print((char)('A' + j));
IO.println();
}
}
}3.20. Pyramid Letters
Input: 5
Output:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBAclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1 - i; j++) IO.print(" ");
for (int j = 0; j <= i; j++) IO.print((char)('A' + j));
for (int j = i - 1; j >= 0; j--) IO.print((char)('A' + j));
IO.println();
}
}
}3.21. Inverted Pyramid Letters
Input: 5
Output:
ABCDEDCBA
ABCDCBA
ABCBA
ABA
Aclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) IO.print(" ");
for (int j = 0; j < n - i; j++) IO.print((char)('A' + j));
for (int j = n - 2 - i; j >= 0; j--) IO.print((char)('A' + j));
IO.println();
}
}
}3.22. Diamond Letters
Input: 5
Output:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDCBA
ABCBA
ABA
Aclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1 - i; j++) IO.print(" ");
for (int j = 0; j <= i; j++) IO.print((char)('A' + j));
for (int j = i - 1; j >= 0; j--) IO.print((char)('A' + j));
IO.println();
}
for (int i = n - 2; i >= 0; i--) {
for (int j = 0; j < n - 1 - i; j++) IO.print(" ");
for (int j = 0; j <= i; j++) IO.print((char)('A' + j));
for (int j = i - 1; j >= 0; j--) IO.print((char)('A' + j));
IO.println();
}
}
}3.23. The Concentric Letter Square
Input: 5
Output:
DDDDDD
DCCCCD
DCBBCD
DCBACD
DCBBCD
DCCCCD
DDDDDDclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
int size = 2 * n - 4;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
int minDist = Math.min(Math.min(i, j), Math.min(size - 1 - i, size - 1 - j));
IO.print((char)('D' - minDist));
}
IO.println();
}
}
}3.24. Continuous Letter Rectangle
Input: 4 5
Output:
ABCDE
FGHIJ
KLMNO
PQRSTclass pattern {
public static void main (String[] args){
String[] parts = IO.readln().split(" ");
int r = Integer.parseInt(parts[0]);
int c = Integer.parseInt(parts[1]);
char ch = 'A';
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
IO.print(ch);
ch++;
}
IO.println();
}
}
}3.25. Continuous Letter Rectangle Reversed
Input: 4 5
Output:
ZYXWV
UTSRQ
PONML
KJIHGclass pattern {
public static void main (String[] args){
String[] parts = IO.readln().split(" ");
int r = Integer.parseInt(parts[0]);
int c = Integer.parseInt(parts[1]);
char ch = 'Z';
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
IO.print(ch);
ch--;
}
IO.println();
}
}
}3.26. Alternating Continuous Letter Rectangle
Input: 4 5
Output:
A B C D E
J I H G F
K L M N O
T S R Q Pclass pattern {
public static void main (String[] args){
String[] parts = IO.readln().split(" ");
int r = Integer.parseInt(parts[0]);
int c = Integer.parseInt(parts[1]);
char ch = 'A';
for (int i = 0; i < r; i++) {
if (i % 2 == 0) {
for (int j = 0; j < c; j++) {
IO.print(ch);
if (j < c - 1) IO.print(" ");
ch++;
}
} else {
ch += c - 1;
for (int j = 0; j < c; j++) {
IO.print(ch);
if (j < c - 1) IO.print(" ");
ch--;
}
ch += c + 1;
}
IO.println();
}
}
}3.27. Twin Picks of Letter
Input: 5
Output:
A A
AB BA
ABC CBA
ABCD DCBA
ABCDEDCBAclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) IO.print((char)('A' + j));
for (int k = 0; k < 2 * (n - 1 - i) - 1; k++) IO.print(" ");
int start = (i == n - 1) ? i - 1 : i;
for (int l = start; l >= 0; l--) IO.print((char)('A' + l));
IO.println();
}
}
}3.28. Inverted Twin Picks of Letter
Input: 5
Output:
ABCDEDCBA
ABCD DCBA
ABC CBA
AB BA
A Aclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) IO.print((char)('A' + j));
for (int k = 0; k < 2 * i - 1; k++) IO.print(" ");
int start = (i == 0) ? n - 2 : n - 1 - i;
for (int l = start; l >= 0; l--) IO.print((char)('A' + l));
IO.println();
}
}
}3.29. Hollow Diamond of Letter Rectangle
Input: 5
Output:
ABCDEDCBA
ABCD DCBA
ABC CBA
AB BA
A A
AB BA
ABC CBA
ABCD DCBA
ABCDEDCBAclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i; j++) IO.print((char)('A' + j));
for (int k = 0; k < 2 * i - 1; k++) IO.print(" ");
int start = (i == 0) ? n - 2 : n - 1 - i;
for (int l = start; l >= 0; l--) IO.print((char)('A' + l));
IO.println();
}
for (int i = 1; i < n; i++) {
for (int j = 0; j <= i; j++) IO.print((char)('A' + j));
for (int k = 0; k < 2 * (n - 1 - i) - 1; k++) IO.print(" ");
int start = (i == n - 1) ? i - 1 : i;
for (int l = start; l >= 0; l--) IO.print((char)('A' + l));
IO.println();
}
}
}3.30. Hollow Letter Square
Input: 5
Output:
ABCDE
F G
H I
J K
LMNOPclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
char ch = 'A';
for (int i = 0; i < n; i++) {
if (i == 0 || i == n - 1) {
for (int j = 0; j < n; j++) { IO.print(ch); ch++; }
} else {
IO.print(ch); ch++;
for (int j = 0; j < n - 2; j++) IO.print(" ");
IO.print(ch); ch++;
}
IO.println();
}
}
}3.31. Hollow Reversed Letter Square
Input: 5
Output:
ZYXWV
U T
S R
Q P
ONMLKclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
char ch = 'Z';
for (int i = 0; i < n; i++) {
if (i == 0 || i == n - 1) {
for (int j = 0; j < n; j++) { IO.print(ch); ch--; }
} else {
IO.print(ch); ch--;
for (int j = 0; j < n - 2; j++) IO.print(" ");
IO.print(ch); ch--;
}
IO.println();
}
}
}3.32. Pyramid with Hollow
Input: 5
Output:
A
B C
D E
F G
HIJKLMNOPclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
char ch = 'A';
for (int i = 0; i < n; i++) {
if (i < n - 1) {
for (int j = 0; j < n - 1 - i; j++) IO.print(" ");
if (i == 0) {
IO.print(ch); ch++;
} else {
IO.print(ch); ch++;
for (int k = 0; k < 2 * i - 1; k++) IO.print(" ");
IO.print(ch); ch++;
}
} else {
for (int k = 0; k < 2 * n - 1; k++) { IO.print(ch); ch++; }
}
IO.println();
}
}
}3.33. Inverted Pyramid with Hollow
Input: 5
Output:
ABCDEFGHI
J K
L M
N O
Pclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
char ch = 'A';
for (int i = 0; i < n; i++) {
if (i == 0) {
for (int k = 0; k < 2 * n - 1; k++) { IO.print(ch); ch++; }
} else {
for (int j = 0; j < i; j++) IO.print(" ");
if (i == n - 1) {
IO.print(ch); ch++;
} else {
IO.print(ch); ch++;
for (int k = 0; k < 2 * (n - 1 - i) - 1; k++) IO.print(" ");
IO.print(ch); ch++;
}
}
IO.println();
}
}
}3.34. Hollow Diamond Letters
Input: 5
Output:
A
B C
D E
F G
H I
J K
L M
N O
Pclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
char ch = 'A';
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1 - i; j++) IO.print(" ");
if (i == 0) {
IO.print(ch); ch++;
} else {
IO.print(ch); ch++;
for (int k = 0; k < 2 * i - 1; k++) IO.print(" ");
IO.print(ch); ch++;
}
IO.println();
}
for (int i = n - 2; i >= 0; i--) {
for (int j = 0; j < n - 1 - i; j++) IO.print(" ");
if (i == 0) {
IO.print(ch); ch++;
} else {
IO.print(ch); ch++;
for (int k = 0; k < 2 * i - 1; k++) IO.print(" ");
IO.print(ch); ch++;
}
IO.println();
}
}
}3.35. Hollow Diamond Letters Reversed
Input: 5
Output:
Z
Y X
W V
U T
S R
Q P
O N
M L
Kclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
char ch = 'Z';
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - 1 - i; j++) IO.print(" ");
if (i == 0) {
IO.print(ch); ch--;
} else {
IO.print(ch); ch--;
for (int k = 0; k < 2 * i - 1; k++) IO.print(" ");
IO.print(ch); ch--;
}
IO.println();
}
for (int i = n - 2; i >= 0; i--) {
for (int j = 0; j < n - 1 - i; j++) IO.print(" ");
if (i == 0) {
IO.print(ch); ch--;
} else {
IO.print(ch); ch--;
for (int k = 0; k < 2 * i - 1; k++) IO.print(" ");
IO.print(ch); ch--;
}
IO.println();
}
}
}3.36. Checkerboard Letters
Input: 5
Output:
A B A B A
B A B A B
A B A B A
B A B A B
A B A B Aclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((i + j) % 2 == 0) IO.print("A");
else IO.print("B");
if (j < n - 1) IO.print(" ");
}
IO.println();
}
}
}3.37. Continuous Rectangle Letters
Input: 3 12
Output:
ABCDEFGHIJKL
MNOPQRSTUVWX
YZABCDEFGHIJclass pattern {
public static void main (String[] args){
String[] parts = IO.readln().split(" ");
int r = Integer.parseInt(parts[0]);
int c = Integer.parseInt(parts[1]);
int idx = 0;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
IO.print((char)('A' + (idx % 26)));
idx++;
}
IO.println();
}
}
}3.38. X Shaped Letters
Input: 5
Output:
A E
B D
C
B D
A Eclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j == i || j == n - 1 - i) {
IO.print((char)('A' + j));
} else {
IO.print(" ");
}
}
IO.println();
}
}
}Section 4: Mixed & Advanced Patterns
4.1. Alternating Letters and Numbers
Input: 5
Output:
ABCDE
12345
ABCDE
12345
ABCDEclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
for (int j = 0; j < n; j++) IO.print((char)('A' + j));
} else {
for (int j = 1; j <= n; j++) IO.print(j);
}
IO.println();
}
}
}4.2. Alternating Letters and Symbols
Input: 9
Output:
ABCDEFGHIABCDEFGHI
$$$$$$$$$
ABCDEFGHI
ABCDEFGHI
$$$$$$$$$
ABCDEFGHI
class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
for (int j = 0; j < n; j++) IO.print((char)('A' + j));
} else if ((i / 2) % 2 == 0) {
for (int j = 0; j < n; j++) IO.print("#");
} else {
for (int j = 0; j < n; j++) IO.print("$");
}
IO.println();
}
}
}4.3. Diagonal Numbers and Symbols
Input: 5
Output:
1@@@@
@2@@@
@@3@@
@@@4@
@@@@5class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j == i) IO.print(i);
else IO.print("@");
}
IO.println();
}
}
}4.4. Diagonal Letters and Symbols
Input: 5
Output:
A....
.B...
..C..
...D.
....Eclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j == i) IO.print((char)('A' + i));
else IO.print(".");
}
IO.println();
}
}
}4.5. Matrix with Letters and Numbers
Input: 5
Output:
1A 2B 3C 4D 5E
1A 2B 3C 4D 5E
1A 2B 3C 4D 5E
1A 2B 3C 4D 5E
1A 2B 3C 4D 5Eclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
IO.print(j + "" + (char)('A' + j - 1));
if (j < n) IO.print(" ");
}
IO.println();
}
}
}4.6. Matrix with Inverted Letters and Numbers
Input: 5
Output:
5A 4B 3C 2D 1E
5A 4B 3C 2D 1E
5A 4B 3C 2D 1E
5A 4B 3C 2D 1E
5A 4B 3C 2D 1Eclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 0; j < n; j++) {
IO.print((n - j) + "" + (char)('A' + j));
if (j < n - 1) IO.print(" ");
}
IO.println();
}
}
}4.7. Matrix with Letters and Numbers Incrementing
Input: 5
Output:
A1 B2 C3 D4 E5
A2 B3 C4 D5 E6
A3 B4 C5 D6 E7
A4 B5 C6 D7 E8
A5 B6 C7 D8 E9class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
IO.print((char)('A' + j - 1) + "" + (i + j - 1));
if (j < n) IO.print(" ");
}
IO.println();
}
}
}4.8. Diagonal Letters and Numbers
Input: 5
Output:
1BCDE
A2CDE
AB3DE
ABC4E
ABCD5class pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j == i) IO.print(i);
else IO.print((char)('A' + j - 1));
}
IO.println();
}
}
}4.9. Diagonal Letters and Numbers Inverted
Input: 5
Output:
ABCD1
ABC2E
AB3DE
A4CDE
5BCDEclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j == n - i + 1) IO.print(n - i + 1);
else IO.print((char)('A' + j - 1));
}
IO.println();
}
}
}4.10. Rectangle with Letters and Numbers
Input: 3 6
Output:
A 1 B 2 C 3
D 4 E 5 F 6
G 7 H 8 I 9class pattern {
public static void main (String[] args){
String[] parts = IO.readln().split(" ");
int r = Integer.parseInt(parts[0]);
int c = Integer.parseInt(parts[1]);
char ch = 'A';
int num = 1;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (j % 2 == 0) {
IO.print(ch);
ch++;
} else {
IO.print(num);
num++;
}
if (j < c - 1) IO.print(" ");
}
IO.println();
}
}
}4.11. Sparse Letters and Numbers
Input: 5
Output:
EDCBA
5DCBA
54CBA
543BA
5432Aclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++) {
IO.print(n - j + 1);
}
for (int j = n - i; j >= 0; j--) {
IO.print((char)('A' + j));
}
IO.println();
}
}
}4.12. Pyramid with Letters and Numbers
Input: 5
Output:
A
B4B
C434C
D43234D
E4321234Eclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
if (i == 1) {
for (int j = 1; j <= n - 1; j++) IO.print(" ");
IO.print("A");
} else {
for (int j = 1; j <= n - i; j++) IO.print(" ");
IO.print((char)('A' + i - 1));
for (int j = n - 1; j >= n - i + 1; j--) IO.print(j);
for (int j = n - i + 2; j <= n - 1; j++) IO.print(j);
IO.print((char)('A' + i - 1));
}
IO.println();
}
}
}4.13. Diamond with Letters and Numbers
Input: 5
Output:
ABCDEDCBA
ABCD1DCBA
ABC212CBA
AB32123BA
A4321234A
AB32123BA
ABC212CBA
ABCD1DCBA
ABCDEDCBAclass pattern {
public static void main (String[] args){
int n = Integer.parseInt(IO.readln());
for (int i = 1; i <= n; i++) {
int border = n - i + 1;
for (int j = 0; j < border; j++) IO.print((char)('A' + j));
if (i > 1) {
for (int j = i - 1; j >= 1; j--) IO.print(j);
for (int j = 2; j <= i - 1; j++) IO.print(j);
for (int j = border - 1; j >= 0; j--) IO.print((char)('A' + j));
} else {
for (int j = border - 2; j >= 0; j--) IO.print((char)('A' + j));
}
IO.println();
}
for (int i = n - 1; i >= 1; i--) {
int border = n - i + 1;
for (int j = 0; j < border; j++) IO.print((char)('A' + j));
if (i > 1) {
for (int j = i - 1; j >= 1; j--) IO.print(j);
for (int j = 2; j <= i - 1; j++) IO.print(j);
for (int j = border - 1; j >= 0; j--) IO.print((char)('A' + j));
} else {
for (int j = border - 2; j >= 0; j--) IO.print((char)('A' + j));
}
IO.println();
}
}
}