20 Alphabet Pattern In Java
In this article, we will learn to create 20 different alphabet pattern in Java with descriptions and complete code.
An alphabet pattern program is a series of alphabets that generate a certain pattern or geometrical shapes like a square or triangle.
These patterns enhance your programming skills and are generated with the help of loops and if-else statements.
- Square alphabet pattern
- Hollow square alphabet pattern
- Left triangle alphabet pattern
- Right triangle alphabet pattern
- Hollow alphabet left triangle pattern
- Hollow alphabet right triangle pattern
- Left down alphabet triangle
- Right down alphabet triangle
- pyramid alphabet pattern
- Hollow pyramid alphabet pattern
- Reverse pyramid alphabet pattern
- Reverse hollow pyramid alphabet pattern
- Diamond alphabet pattern
- Hollow diamond alphabet pattern
- Hourglass alphabet pattern
- Right Pascal alphabet pattern
- Left Pascal alphabet pattern
- Heart alphabet pattern
- Plus alphabet pattern
- Cross alphabet pattern
1. Alphabet Square pattern
The square alphabet pattern is a square shape created using alphabets. Here we are using capital letters.
ABCDE ABCDE ABCDE ABCDE ABCDE
It is the easiest pattern to create because it simply needs 2 loops to create the pattern.
Take a size of square (you can also take user input) and create a loop to create the pattern. Use another variable 'alpha' to store 65 which is the ASCII value of 'A'.
Now use a nested loop and print the alphabets by typecasting the values to characters.
public class square {
public static void main(String args[]) {
// size
int size = 5;
int alpha = 65;
// printing square
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
System.out.print((char)(alpha+j));
}
System.out.println();
}
}
}
Output:
ABCDE ABCDE ABCDE ABCDE ABCDE
2. Hollow Alphabet Square Pattern
The hollow square alphabet pattern is the same as the alphabet square pattern but it has alphabets only at its boundaries.
ABCDE A E A E A E ABCDE
It is a bit difficult to create this pattern because you have to create a program that only prints the alphabets at boundaries.
To do this create a nested loop where the internal loop checks if it is first or the last row then print alphabets, if it is not then prints alphabets only at the first and last position and at the rest of the positions print space.
public class hollowSquare {
public static void main(String[] args) {
// size
int size = 5;
int alpha = 65;
// external loop
for (int i = 0; i < size; i++) {
// internal loop
for (int j = 0; j < size; j++) {
// print only alphabets in first and last row
if (i == 0 || i == size - 1) {
System.out.print((char)(alpha+j));
} else {
// print alphabets at first and last positon of row
if (j == 0 || j == size - 1) {
System.out.print((char)(alpha+j));
} else {
System.out.print(" ");
}
}
}
// num = 1;
System.out.println();
}
}
}
Output:
ABCDE A E A E A E ABCDE
3. Left Triangle Alphabet Pattern
The left triangle alphabet pattern has a shape of a triangle with perpendicular on the left side made up of alphabets.
A AB ABC ABCD ABCDE
This is also simple with just one logic in mind. Execute the internal loop for the number of times as the external loop has executed and then print the alphabet.
public class leftTrianlge {
public static void main(String[] args) {
// size
int size = 5;
int alpha = 65;
// printing pattern
for (int i = 0; i < size; i++) {
for (int j = 0; j <= i; j++) {
System.out.print((char)(alpha+j));
}
System.out.println();
}
}
}
Output:
A AB ABC ABCD ABCDE
4. Right Triangle Alphabet Pattern
The right triangle alphabet pattern is an alphabet pattern that has a shape of a triangle with perpendicular to the right side.
A AB ABC ABCD ABCDE
This is the same as the above pattern but print spaces before printing the alphabet which will make it look like a right triangle.
public class rightTrianlge {
public static void main(String[] args) {
// size
int size = 5;
int alpha = 65;
// printing pattern
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 1; j < size - i; j++) {
System.out.print(" ");
}
// print alphabets
for (int k = 0; k <= i; k++) {
System.out.print((char)(alpha+k));
}
System.out.println();
}
}
}
Output:
A AB ABC ABCD ABCDE
5. Hollow Alphabet Left Triangle
The hollow left triangle alphabet pattern is a left triangle alphabet pattern with alphabets only at its boundaries.
A AB A C A D ABCDE
To create this pattern, print only alphabets in the last row, and in other rows print alphabets only at the first and last position.
public class hollowLeftTrianlge {
public static void main(String[] args) {
int size = 5;
int alpha = 65;
for (int i = 1; i <= size; i++) {
for (int j = 0; j < i; j++) {
// not last row
if (i != size) {
if (j == 0 || j == i - 1) {
System.out.print((char)(alpha+j));
} else {
System.out.print(" ");
}
}
// last row, print only alphabets
else {
System.out.print((char)(alpha+j));
}
}
System.out.println();
}
}
}
Output:
A AB A C A D ABCDE
6. Hollow Alphabet Right Triangle
The hollow right triangle alphabet pattern is the same as the right triangle alphabet pattern but it has alphabets only at its boundaries.
A AB A C A D ABCDE
You can use the same logic as creating a hollow left triangle pattern but print spaces for a number of times as the size of the triangle minus the row number.
public class hollowRightTrianlge {
public static void main(String[] args) {
int size = 5;
int alpha = 65;
for (int i = 1; i <= size; i++) {
// printing space
for (int j = 1; j <= size - i; j++) {
System.out.print(" ");
}
// printing alphabets
for (int j = 0; j < i; j++) {
// not last row
if (i != size) {
if (j == 0 || j == i - 1) {
System.out.print((char)(alpha+j));
} else {
System.out.print(" ");
}
}
// last row
else {
System.out.print((char)(alpha+j));
}
}
System.out.println();
}
}
}
Output:
A AB A C A D ABCDE
7. Left Down Alphabet Triangle
The left down alphabet triangle looks like the water image of the left alphabet triangle.
ABCDE ABCD ABC AB A
Here is complete code for this pattern:
public class leftDown {
public static void main(String[] args) {
int size = 5;
int alpha = 65;
for (int i = 0; i < size; i++) {
// printing alphabets
for (int j = 0; j < size - i; j++) {
System.out.print((char)(alpha+j));
}
System.out.println();
}
}
}
Output:
ABCDE ABCD ABC AB A
8. Right Down Alphabet Triangle
The right down alphabet triangle looks like the water image of the right alphabet triangle.
ABCDE ABCD ABC AB A
Here is complete code for right down alphabet triangle pattern:
public class rightDown {
public static void main(String[] args) {
int size = 5;
int alpha = 65;
for (int i = 0; i < size; i++) {
// printing spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// printing alphabets
for (int j = 0; j < size - i; j++) {
System.out.print((char)(alpha+j));
}
System.out.println();
}
}
}
Output:
ABCDE ABCD ABC AB A
9. Pyramid Alphabet Pattern
The pyramid alphabet pattern is an alphabet pattern that has a pyramid shape. It is quite famously created in programming.
A ABC ABCDE ABCDEFG ABCDEFGHI
The pyramid pattern has an odd number of elements in each row. Following patterns like 1, 3, 5, 7, etc.
Her is complete code for pyramid alphabet pattern:
public class pyramid {
// pyramid alphabet pattern
public static void main(String[] args) {
int size = 5;
int alpha = 65;
for (int i = 0; i < size; i++) {
// printing spaces
for (int j = 0; j < size-i-1; j++) {
System.out.print(" ");
}
// printing alphabets
for (int k = 0; k < 2 * i + 1; k++) {
System.out.print((char)(alpha+k));
}
System.out.println();
}
}
}
Output:
A ABC ABCDE ABCDEFG ABCDEFGHI
10. Hollow Pyramid Alphabet Pattern
The hollow pyramid alphabet pattern is the same as the pyramid pattern but hollow inside.
A A C A E A G ABCDEFGHI
Here is complete code for hollow pyramid alphabet pattern:
public class hollowPyramid {
public static void main(String[] args) {
int size = 5;
int alpha = 65;
for (int i = 0; i < size; i++) {
// printing spaces
for (int j = 0; j < size - i - 1; j++) {
System.out.print(" ");
}
// printing alphabets
for (int k = 0; k < 2 * i + 1; k++) {
if (i == 0 || i == size - 1) {
System.out.print((char)(alpha+k));
} else {
if (k == 0 || k == 2 * i) {
System.out.print((char)(alpha+k));
} else {
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
Output:
A A C A E A G ABCDEFGHI
11. Reverse Pyramid Alphabet Pattern
The reverse pyramid alphabet pattern is nothing but a pyramid pattern rotated 180 degrees.
ABCDEFGHI ABCDEFG ABCDE ABC A
You can see below the complete code to create a reverse pyramid alphabet pattern in Java:
public class reversePyramid {
public static void main(String[] args) {
// size of the pyramid
int size = 5;
int alpha = 65;
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// print alphabets
for (int k = 0; k < 2 * (size - i) - 1; k++) {
System.out.print((char)(alpha+k));
}
System.out.println();
}
}
}
Output:
ABCDEFGHI ABCDEFG ABCDE ABC A
12. Reverse Hollow Pyramid Alphabet Pattern
The reverse hollow pyramid alphabet pattern water image of hollow alphabet pyramid pattern.
ABCDEFGHI A G A E A C A
Here is complete code to create a reverse hollow pyramid alphabet pattern in Java:
public class reverseHollowPyramid {
public static void main(String[] args) {
int size = 5;
int alpha = 65;
for (int i = 0; i < size; i++) {
// printing spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// printing alphabets
for (int k = 0; k < 2 * (size - i) - 1; k++) {
if (i == 0 || i == size - 1) {
System.out.print((char)(alpha+k));
} else {
if (k == 0 || k == 2 * (size - i - 1)) {
System.out.print((char)(alpha+k));
} else {
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
Output:
ABCDEFGHI A G A E A C A
13. Diamond Alphabet Pattern
Here is what the diamond alphabet pattern in Java looks like.
A ABC ABCDE ABCDEFG ABCDEFGHI ABCDEFG ABCDE ABC A
You can observe that the diamond pattern is made up of 2 sections the upper section is a pyramid pattern and the lower section is a reverse pyramid pattern.
The complete code of the alphabet diamond pattern is given below.
public class diamond {
public static void main(String[] args) {
int size = 5;
int alpha = 65;
// upper pyramid
for (int i = 1; i <= size; i++) {
// printing spaces
for (int j = size; j > i; j--) {
System.out.print(" ");
}
// printing alphabets
for (int k = 0; k < i * 2 - 1; k++) {
System.out.print((char)(alpha+k));
}
System.out.println();
}
// lower pyramid
for (int i = 1; i <= size - 1; i++) {
// printing spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// printing alphabets
for (int k = 0; k < (size - i) * 2 - 1; k++) {
System.out.print((char)(alpha+k));
}
System.out.println();
}
}
}
Output:
A ABC ABCDE ABCDEFG ABCDEFGHI ABCDEFG ABCDE ABC A
14. Hollow Diamond Alphabet Pattern
The diamond hollow alphabet pattern is just a diamond pattern where the middle section is made of spaces.
A A C A E A G A I A G A E A C A
Here is the complete code to create a hollow diamond alphabet pattern in Java:
public class hollowDiamond {
public static void main(String[] args) {
int size = 5;
int alpha = 65;
// upper pyramid
for (int i = 1; i <= size; i++) {
// printing spaces
for (int j = size; j > i; j--) {
System.out.print(" ");
}
// printing alphabets
for (int k = 0; k < i*2-1; k++) {
if (k == 0 || k == 2*i-2) {
System.out.print((char)(alpha+k));
} else {
System.out.print(" ");
}
}
System.out.println();
}
// lower triangle
for (int i = 1; i < size; i++) {
// printing spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// printing alphabets
for (int k = 0; k <= (size-i-1)*2; k++) {
if (k == 0 || k == (size-i-1)*2) {
System.out.print((char)(alpha+k));
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Output:
A A C A E A G A I A G A E A C A
15. Hourglass Alphabet Pattern
The hourglass alphabet pattern has a shape of hourglass made up of alphabets.
ABCDEFGHI ABCDEFG ABCDE ABC A ABC ABCDE ABCDEFG ABCDEFGHI
The complete code of the alphabet hourglass pattern is given below.
public class hourGlass {
public static void main(String[] args) {
int size = 5;
int alpha = 65;
// reversed pyramid alphabet pattern
for (int i = 0; i < size; i++) {
// printing spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// printing alphabets
for (int k = 0; k < (size - i) * 2 - 1; k++) {
System.out.print((char)(alpha+k));
}
System.out.println();
}
// pyramid alphabet pattern
for (int i = 2; i <= size; i++) {
// printing spaces
for (int j = size; j > i; j--) {
System.out.print(" ");
}
// printing alphabet
for (int k = 0; k < i * 2 - 1; k++) {
System.out.print((char)(alpha+k));
}
System.out.println();
}
}
}
Output:
ABCDEFGHI ABCDEFG ABCDE ABC A ABC ABCDE ABCDEFG ABCDEFGHI
16. Alphabet Right Pascal Triangle Pattern
Given below is the right pascal triangle alphabet pattern.
A AB ABC ABCD ABCDE ABCD ABC AB A
The complete code of the alphabet right pascal triangle pattern is given below.
public class leftPascal {
public static void main(String[] args) {
// left pasal triangle
int size = 5;
int alpha = 65;
for (int i = 1; i <= size; i++) {
for (int j = 0; j < i; j++) {
System.out.print((char)(alpha+j));
}
System.out.println();
}
for (int i = 1; i <= size - 1; i++) {
for (int j = 0; j < size - i; j++) {
System.out.print((char)(alpha+j));
}
System.out.println();
}
}
}
Output:
A AB ABC ABCD ABCDE ABCD ABC AB A
17. Alphabet Left Pascal Triangle Pattern
Given below is the left pascal triangle alphabet pattern.
A AB ABC ABCD ABCDE ABCD ABC AB A
The complete code of the alphabet left pascal triangle pattern is given below.
public class leftPascal {
public static void main(String[] args) {
// left pasal triangle
int size = 5;
int alpha = 65;
for (int i = 1; i <= size; i++) {
for (int j = 0; j < size - i; j++) {
System.out.print(" ");
}
for (int k = 0; k < i; k++) {
System.out.print((char)(alpha+k));
}
System.out.println();
}
for (int i = 1; i <= size - 1; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int k = 0; k < size - i; k++) {
System.out.print((char)(alpha+k));
}
System.out.println();
}
}
}
Output:
A AB ABC ABCD ABCDE ABCD ABC AB A
18. Alphabet Heart Pattern In Java
The alphabet heart pattern is a very complex pattern to create.
AB AB ABCD ABCD ABCDEFGHI ABCDEFG ABCDE ABC A
Complete code to create aalphabet heart pattern in Java is given below:
public class heart {
public static void main(String[] args) {
// heart alphabet pattern
int size = 5;
int alpha = 65;
for (int i = size / 2; i < size; i += 2) {
// print first spaces
for (int j = 1; j < size - i; j += 2) {
System.out.print(" ");
}
// print first alphabets
for (int j = 1; j < i + 1; j++) {
System.out.print((char)(alpha+j-1));
}
// print second spaces
for (int j = 1; j < size - i + 1; j++) {
System.out.print(" ");
}
// print second alphabets
for (int j = 1; j < i + 1; j++) {
System.out.print((char)(alpha+j-1));
}
System.out.println();
}
// lower part
// inverted pyramid
for (int i = size; i > 0; i--) {
for (int j = 0; j < size - i; j++) {
System.out.print(" ");
}
for (int j = 1; j < i * 2; j++) {
System.out.print((char)(alpha+j-1));
}
System.out.println();
}
}
}
Output:
AB AB ABCD ABCD ABCDEFGHI ABCDEFG ABCDE ABC A
19. Alphabet Plus Pattern In Java
The alphabet plus pattern is a pattern made up of alphabets having the shape of a mathematical symbol plus (+).
A B ABCDE E F
Complete code to create alphabet plus pattern in Java is given below:
public class plus {
public static void main(String[] args) {
// size of plus, use odd number
int size = 5;
int alphaH = 65; // alphabet horizontal
int alphaV = 65; // alphabet vertical
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
// print only stars in middle row
if (i == size / 2) {
System.out.print((char)(alphaH+j));
}
// other than middle row, print alphabets only at index size/2
else {
if (j == size / 2) {
System.out.print((char)(alphaV+i));
} else {
System.out.print(" ");
}
}
}
if (i == size / 2) {
alphaV++;
}
System.out.println();
}
}
}
Output:
A B ABCDE E F
20. Alphabet X Pattern In Java
The alphabet X pattern is a pattern made up of alphabets having a cross shape (X).
A E B D C B D A E
Complete code to create aalphabet X pattern in Java is given below:
public class cross {
public static void main(String[] args) {
// size of cross, use odd number
int size = 5;
int alpha = 65;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i == j || i + j == size - 1) {
System.out.print((char)(alpha+j));
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Output:
A E B D C B D A E
Conclusion
In this article, you learned and created 20 different alphabet pattern in Java. You can check out alphabet patterns in Javascript and alphabet patterns in Python.