Pattern Programs in Java: Print Star, Pyramid & Number
⚡ Smart Summary
Pattern programs in Java print stars, numbers, or letters in a fixed shape, and every one of them is built from nested loops where the outer loop counts rows and the inner loops place spaces and characters.
Pattern printing programs are designs or symbols that include letters or other characters in a specific format. Java pattern coding increases the logic building of the programmers which helps them become good programmers.
Every example here needs only nested loops and basic Java syntax.
Categories of patterns
We can draw uncountable patterns in Java, and all patterns can be executed in three different ways.
There are three main categories used to identify Java pattern programs.
- Star pattern: In Java star pattern programs, the star patterns are a series of asterisks (*) used to create different shapes like a pyramid star pattern program, tight triangle, left triangle, diamond shape, square, and many more patterns in Java.
- Number pattern: To practice flow control statements, another pattern type is number pattern programs in Java. Number pattern programs consist of numbers/counting. Mostly we set the value of ‘i’, which is a loop iterator to generate numbers in pattern programming. The least number of loops for the number pattern is also two. The number pattern program in Java also works on the rows and column system.
- Character pattern: The character pattern programming consists of characters that are in the English alphabets. This is another way to enhance your coding skills, but here we deal with characters, not with numbers or stars. The pattern can have the same character in the whole pattern or can have different characters it depends on the logic you apply.
The table below shows what changes between the categories.
| Category | Printed unit | Where the printed value comes from | Sample output |
|---|---|---|---|
| Star pattern | Asterisk | A fixed symbol, so only the loop bounds change | * * * |
| Number pattern | Digit | The loop iterator i or j printed directly | 1 2 3 |
| Character pattern | Letter | An int seed cast to char, starting at 65 for A | A B C |
In all three cases the outer loop counts rows, one inner loop prints aligning spaces, and a second prints the visible characters. Only the printed unit changes.
The same loop skills drive other classic exercises such as selection sort, insertion sort, and the Armstrong number program.
Star patterns come first, because the symbol never changes and only the loop bounds matter.
Star Patterns Programs in Java
Star patterns print an asterisk on the console, the pattern shape entirely depends on the code you write.
Half left triangle star pattern program:
The program below prints this half left triangle:
public class LeftStarPattern{ public static void main(String args[]){ int i, j, row = 6; for (i=0; i<row; i++) { for (j=2*(row-i); j>=0; j--) { System.out.print(" "); } for (j=0; j<=i; j++ ) { System.out.print("* "); } System.out.println(); } } }
Code explanation:
- Declare int i, int j. Declare and initialize the row variable as an int row.
- i is the iterator for the outer ‘for’ loop and, j is an iterator for the inner ‘for’ loop, ‘row’ contains a number of the rows the pyramid should have.
- In nested for, the outer ‘for’ represents rows, and the inner ‘for’ represents columns.
- In the outer ‘for’, initialize the iterator ‘i’ with 0, set the condition i< row, and increment i.
- Now the inner ‘for’ loop is initialized with 2*(row-i) and decrements.
- The nested ‘for’ prints space on the console screen.
- Outside the body of nested ‘for’ another ‘for’ loop is placed. This ‘for’ print star after the nested ‘for’ is executed. It loops until j<=i.
Sandglass star pattern program:
Two stacked pyramids produce this sandglass:
public class Main { public static void main(String[] args) { int rows = 5; for (int i= 0; i<= rows-1 ; i++) { for (int j=0; j <i; j++) { System.out.print(" "); } for (int k=i; k<=rows-1; k++) { System.out.print("*" + " "); } System.out.println(""); } for (int i= rows-1; i>= 0; i--) { for (int j=0; j< i ;j++) { System.out.print(" "); } for (int k=i; k<=rows-1; k++) { System.out.print("*" + " "); } System.out.println(""); } } }
Code explanation:
- Code starts with the public static void main, int rows will define the number of rows sand glass will contain.
- There are two nested ‘for’, the first will display the downward pyramid, and the second will display the upward pyramid.
- In nested for, the outer ‘for’ represents rows, and the inner ‘for’ represents columns.
- In the outer ‘for’ loops until i<= row-1, inner ‘for’ loops until i. print system prints the space on the console.
- The second inner loop prints an asterisk plus space on the console.
- When the first inner loop is executed fully at i=0 after that second inner loop is executed fully.
- Then the outer loop incremented. This cycle goes on until the condition turns false.
- The second nested loop displays the upward pyramid, just changing the initialization and condition of the outer loop of the first nested loop.
- It will display the upward pyramid. Both downward and upward pyramids will make the sandglass star pattern.
Diamond shape star pattern program:
An upward and a downward pyramid give this diamond:
public class Main { public static void main(String args[]) { int n, i, j, space_0 = 1; n = 6; space_0 = n - 1; for (j = 1; j<= n; j++) { for (i = 1; i<= space_0; i++) { System.out.print(" "); } space_0--; for (i = 1; i <= 2 * j - 1; i++) { System.out.print("*"); } System.out.println(""); } space_0 = 1; for (j = 1; j<= n - 1; j++) { for (i = 1; i<= space_0; i++) { System.out.print(" "); } space_0++; for (i = 1; i<= 2 * (n - j) - 1; i++) { System.out.print("*"); } System.out.println(""); } } }
Code explanation:
- In the diamond star pattern, ‘n’ is the number of rows, stored n-1 in space_0.
- In nested for, the outer ‘for’ represents rows, and the inner ‘for’ represents columns.
- The first nested ‘for’ displays the upward pyramid. Outer ‘for’ loops till n and inner for loops till space_0 inner loop displays space on the console.
- There is another inner ‘for’ that loops until i<= 2 * j – 1 and displays a star outside this inner ‘for’ print system prints a line. This nested loop prints the upper pyramid.
- The other nested ‘for’ displays a downward pyramid. Outer ‘for’ loops until i<=n-1, first inner ‘for’ prints space and second inner for prints star.
- Outside the inner ‘for’, print line space. both upward and downward pyramid makes the diamond pattern.
Right triangle star pattern space pattern program:
The modulus of j and i inserts these gaps:
public class AsterikProgramJava { public static void main(String[] args) { for(int i=1;i<=10;i++) { for(int j=1;j<=i+i;j++) { int mid; mid=j%i; if(mid==0) System.out.print("* "); else System.out.print("*"); } System.out.print("\n"); } } }
Code explanation:
- In the above pattern code, the outer ‘for’ takes care of the number of rows, and the inner ‘for’ maintains the columns.
- When outer ‘for’ is 1, which means the first row, and when inner ‘for’ is 1, that means it is the first column.
- Inside the inner ‘for’, declare a variable named mid and store the remainder of j/i. This divides the i and j values to get the modulus.
- So, if the modulus is 0, then display an asterisk and space; otherwise, display an asterisk only. Outside the inner ‘for’, print a line.
Modern Java note: from Java 11 the space loops can be replaced by System.out.print(" ".repeat(n)), and building each row in a StringBuilder before one println() call runs faster. The original code is kept as written, because it shows the arithmetic explicitly.
Swapping the asterisk for the loop counter turns each star shape into a number shape.
Numeric Patterns Programs in Java
Numeric patterns consist of numbers; we can draw many different shapes from numeric patterns.
Opposite half triangle pattern program:
Counting down, then up, gives this number triangle:
public class Number_p { public static void main(String[] args) { int i, j, rows_0=5; for (i = rows_0; i >= 1; i--) { for (j = 1; j <= i; j++) { System.out.print(j+" "); } System.out.println(); } for (i = 2; i <= rows_0; i++) { for (j = 1; j <= i; j++) { System.out.print(j+" "); } System.out.println(); } } }
Code explanation:
- To draw this pattern, we will use two nested ‘for’ loops. One will print the first pyramid, and the other will display the second pyramid.
- Declare int i, int j, and int row_0, and initialize the row by the number of rows you want. Here row value is 5.
- Set the outer ‘for’ loop of the first nested ‘for’, initialize i by the number of rows, and iterate until i >= and decrement the iterator. This outer ‘for’ deals with rows.
- In inner ‘for’ set the condition j=1 and iterate until j<=i. In the body of the inner loop, display the j variable with the space.
- Now in the second nested ‘for’, change the outer ‘for’ condition of the first nested ‘for’. Initialize it by 2 and loop until the i<= rows.
- And inner loop iterates until i is reached. Then displays the message on the console, the same as in the inner loop of the first nested ‘for’ outside the body of the inner for prints line space.
Repeated Number in column right triangle Pattern Program:
Resetting the counter on each row repeats the numbers:
public class Number_pattern { public static void main(String args[]) { int i, j,number, n=7; for(i=0; i<n; i++) { number=1; for(j=0; j<=i; j++) { System.out.print(number+ " "); number++; } System.out.println(); } } }
Code explanation:
- In the example code, declare int i, int j, int number, and int n.
- Then initialize the n with the value of the number of rows you want, here it is 7.
- In the outer ‘for’, start the loop from value 0 and iterate until n is reached.
- You can initialize the number inside or outside the loop.
- In the inner ‘for’, iterate until j is less than or equal to i reached. And display the number with the space.
- Outside the body of the inner ‘for’, print line space.
- The inner ‘for’ iterates until its condition turns false. When this condition turns false, the outer loop increments again and executes the inner ‘for’ until the condition turns false.
- This procedure repeats itself till the outer loop turns false.
The same skeleton prints letters once an int seed is cast to char.
Character Patterns Programs in Java
Character patterns consist of English alphabets. We can make cool and awesome character pattern programs in Java a few are discussed below.
Character Half pyramid pattern program:
Casting an int seed to char prints these letters:
public class Character_p{
public static void main(String[] args){
int alphabet = 65;
for (int i = 0; i <= 5; i++){
for (int j = 0; j <= i; j++)
{
System.out.print((char) alphabet + " ");}
alphabet++;
System.out.println();
}
}
}
Code explanation:
- Declare the alphabet and initialize it with the value 65.
- The outer for start the loop from 0 and repeats until i is 5 or less.
- Inside the for, the loop starts at 0 and repeats until j is less than or equal to i.
- Then print the character and the space on the output screen.
- To print characters, we typecast the alphabet from integer to character.
- Outside its body, increment the alphabet and print the line on the output screen. The (char) converts the alphabet value to a character.
Pyramid/triangle pattern in Java:
A space loop centres the letters into a pyramid:
public class CharacterPattern
{
public static void main(String[] args){
for (int i = 0; i <= 8; i++)
{
int alphabet_0 = 65;
for (int j = 8; j > i; j--)
{
System.out.print(" ");
}
for (int k = 0; k <= i; k++)
{
System.out.print((char) (alphabet_0 + k) + " ");
}
System.out.println();
}
}
}
Code explanation:
- In this example code, we used three ‘for’ loops, one is an outer loop, and two loops are nested inside the outer ‘for’.
- The outer ‘for’ starts from 0 and loops until the value is 8 or less.
- In the body of the outer ‘for’, we initialized the alphabet variable of integer type with a value of 65 and nested the other ‘for’ in its body.
- The first inner ‘for’ is the reverse of the outer ‘for’ loop, and in its body, print space on the console. Outside its body, there is another ‘for’. It iterates like the outer loop.
- In the body of the second inner loop, print the character by type-casting the alphabet +k and print it with space on the console. Outside the body of the second inner ‘for’ print a line.








