Print A to Z Alphabet Using Stars in Java (2026)
Last Updated: | Author: Deep Singh
Printing alphabet patterns with stars is one of the classic exercises every Java beginner runs into. It looks simple — just print some asterisks in a grid — but it teaches you something important: how to translate a visual shape into a set of conditional rules inside nested loops.
In this tutorial, you'll find the complete Java code to print every letter from A to Z using stars. Each letter has its own method, so you can copy just the ones you need. Every code block shows the exact output it produces.
How it works: Each letter is drawn on a 5×5 grid (except K, which uses a 7×5 grid for its diagonal). We loop over every row and column, and at each cell we check a set of conditions — like "is this the first row?" or "is this the left edge?" — to decide whether to print a star or a space.
Letter A
void a() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == 0 || j == 0 || j == 4 || i == 2) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
*****
* *
*****
* *
* *
Letter B
void b() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == 0 || j == 0 || j == 4 || i == 2 || i == 4) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
*****
* *
*****
* *
*****
Letter C
void c() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == 0 || j == 0 || i == 4) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
*****
*
*
*
*****
Letter D
void d() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == 0 || j == 0 || j == 4 || i == 4) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
*****
* *
* *
* *
*****
Letter E
void e() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == 0 || j == 0 || i == 2 || i == 4) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
*****
*
*****
*
*****
Letter F
void f() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == 0 || j == 0 || i == 2) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
*****
*
*****
*
*
Letter G
void g() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == 0 || j == 0 || i == 4 ||
(j == 4 && i >= 2) || (i == 2 && j > 2)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
*****
*
* **
* *
*****
Letter H
void h() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == 0 || j == 4 || i == 2) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
* *
* *
*****
* *
* *
Letter I
void i() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == 0 || j == 2 || i == 4) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
*****
*
*
*
*****
Letter J
void j() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == 0 || j == 2 || (i == 4 && j <= 2)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
*****
*
*
*
***
Letter K
K uses a 7-row grid because the diagonal needs more vertical space.
void k() {
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 5; j++) {
if (j == 0 || (3 - j) == i || (3 + j) == i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
* *
* *
**
*
**
* *
* *
Letter L
void l() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == 0 || i == 4) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
*
*
*
*
*****
Letter M
void m() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == 0 || j == 4 ||
(i == j && i <= 2) ||
(i + j == 4 && i <= 2)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
* *
** **
* * *
* *
* *
Letter N
void n() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == 0 || j == 4 || i == j) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
* *
** *
* * *
* **
* *
Letter O
void o() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == 0 || j == 4 || i == 0 || i == 4) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
*****
* *
* *
* *
*****
Letter P
void p() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == 0 || i == 0 || i == 2 || (j == 4 && i <= 2)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
*****
* *
*****
*
*
Letter Q
void q() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == 0 || i == 0 || i == 4 || j == 4 ||
(i == j && j >= 2)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
*****
* *
* * *
* **
*****
Letter R
void r() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == 0 || i == 0 || i == 2 ||
(i == j && i > 2) || (j == 4 && i <= 2)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
*****
* *
*****
* *
* *
Letter S
void s() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == 0 || i == 2 || i == 4 ||
(j == 0 && i <= 2) || (j == 4 && i >= 2)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
*****
*
*****
*
*****
Letter T
void t() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == 0 || j == 2) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
*****
*
*
*
*
Letter U
void u() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == 0 || j == 4 || i == 4) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
* *
* *
* *
* *
*****
Letter V
void v() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if ((i == j || i + j == 4) && i <= 2) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
* *
* *
*
Tip: If you want the point of the V to reach the bottom, extend the loop to 5 and use i <= 2 with an extra condition like i == 3 && j == 2.
Letter W
void w() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == 0 || j == 4 || i == 4 ||
(j == 2 && i >= 2)) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
* *
* *
* * *
* * *
*****
Letter X
void x() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == j || i + j == 4) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
* *
* *
*
* *
* *
Letter Y
void y() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if ((i == j && i <= 2) || i + j == 4) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
* *
* *
*
*
*
Letter Z
void z() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (i == 0 || i == 4 || i + j == 4) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
Output:
*****
*
*
*
*****
Wrapping Up
You now have the complete Java code to print every letter from A to Z using stars. Each letter is drawn on a 5×5 grid (K uses 7×5) by checking a small set of row and column conditions inside nested loops.
A few things worth noting:
- The patterns are exercises, not production code. They're great for understanding loops and conditionals, and they come up often in coding interviews.
- Every letter can be modified. Change the grid size, tweak the conditions, and you can produce bold, italic, or differently-styled letters.
- Try stacking letters. Print multiple letters side by side by adding an extra loop that runs the pattern for each letter and joining them horizontally.
- Break the problem down. If you're stuck on a letter, sketch it on graph paper first. Each star becomes a condition, each blank a fall-through.
No comments:
Post a Comment