Pattern printing is an essential exercise for anyone learning Java programming. It not only helps solidify your understanding of loops but also enhances your problem-solving skills.
In this article, we will explore 25 different patterns, starting from basic ones and moving to more complex ones, with fully explained Java code examples.
These patterns will give you hands-on experience with different types of loops and will help you develop a deeper understanding of Java. Whether you’re a beginner or looking to sharpen your skills, this guide is perfect for you.
Table of Contents
1. Square Fill Pattern
The Square Fill Pattern is one of the simplest and most fundamental patterns. It prints a square made of asterisks (*
).
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output Example:
*****
*****
*****
*****
*****
2. Right Half Pyramid Pattern
This pattern starts with one *
in the first row and increases the number of *
by one with each subsequent row.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output Example:
*
**
***
****
*****
3. Reverse Right Half Pyramid Pattern
This pattern is similar to the right-half pyramid but inverted.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n - i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output Example:
******
*****
****
***
**
*
4. Number Increasing Pyramid Pattern
This pattern prints numbers in a pyramid shape, starting from 1
in each row.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
Output Example:
1
12
123
1234
12345
5. Number-Increasing Reverse Pyramid Pattern
This is the reverse version of the previous pattern, with numbers decreasing in each row.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n - i + 1; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
Output Example:
12345
1234
123
12
1
6. Right Pascal’s Triangle
This pattern forms a diamond-like shape using asterisks, first increasing and then decreasing in size.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
// Upper half
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
// Lower half
for(int i = 1; i < n; i++) {
for(int j = i; j < n; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Output Example:
*
* *
* * *
* * * *
* * *
* *
*
7. K Pattern
This pattern creates a shape resembling the letter “K” using asterisks.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
// Upper half of K
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n - i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
// Lower half of K
for(int i = 2; i <= n; i++) {
for(int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output Example:
******
*****
****
***
**
*
**
***
****
*****
******
8. Zero-One Triangle Pattern
This pattern alternates between 1
and 0
, creating a triangular shape.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
System.out.print((i + j) % 2 + " ");
}
System.out.println();
}
}
}
Output Example:
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
9. Square Hollow Pattern
A hollow square is created where only the borders are printed with *
.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= n; j++) {
if(i == 0 || j == 0 || i == n || j == n) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Output Example:
******
* *
* *
* *
* *
******
10. Number-changing Pyramid Pattern
The number-changing pyramid pattern is a simple exercise that helps beginners get comfortable with loops and number patterns.
Pattern:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
int num = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
}
11. Left Half Pyramid Pattern
This pattern helps you understand how to print spaces and stars together to form half of a pyramid.
Pattern:
*
**
***
****
*****
******
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
12. Reverse Left Half Pyramid Pattern
This pattern is the reverse of the left half pyramid and is a good practice for understanding loops in reverse order.
Pattern:
******
*****
****
***
**
*
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= n - i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
13. Triangle Star Pattern
The triangle star pattern helps you learn how to print stars in an increasing pattern, forming a perfect triangle shape.
Pattern:
*
***
*****
*******
*********
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//Square hallow pattern
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
for(int i=1;i<=n;i++){
//for printing the spaces
for(int j=1;j<=n-i;j++){
System.out.print(" ");
}
//for printing the stars
for(int j=1;j<=i*2-1;j++){
System.out.print("*");
}
System.out.println();
}
}
}
14. Number Triangle Pattern
This pattern is an excellent way to practice printing numbers in a triangular form.
Pattern:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
}
}
15. Reverse Number Triangle Pattern
This reverse number pattern starts from a larger number and works its way down.
Pattern:
1 2 3 4 5 6
2 3 4 5 6
3 4 5 6
4 5 6
5 6
6
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int j = i; j <= n; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
16. Rhombus Pattern
A rhombus pattern is a great way to practice the combination of spaces and stars to form a symmetrical shape.
Pattern:
******
******
******
******
******
******
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= n; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
17. Palindrome Triangle Pattern
This pattern prints numbers in a mirrored way, making it a bit more advanced.
Pattern:
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//Square hallow pattern
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
// Print leading spaces
for (int j = i; j < n; j++) {
System.out.print(" ");
}
// Print decreasing numbers
for (int j = i; j >= 1; j--) {
System.out.print(j + " ");
}
// Print increasing numbers
for (int j = 2; j <= i; j++) {
System.out.print(j + " ");
}
// Move to the next line
System.out.println();
}
}
}
18. Diamond Star Pattern
The diamond pattern is one of the most popular and visually appealing patterns. It requires a good understanding of both the upper and lower triangles.
Pattern:
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= n - 1; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(" ");
}
for (int j = 1; j <= ((n - i) * 2 - 1); j++) {
System.out.print("*");
}
System.out.println();
}
}
}
19. Butterfly Star Pattern
This pattern creates a butterfly-like design using stars (*
). It’s symmetrical and has two halves, an upper and lower part, created using nested loops for stars and spaces.
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
// Upper part
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) System.out.print("*");
for(int j = 1; j <= (n-i)*2; j++) System.out.print(" ");
for(int j = 1; j <= i; j++) System.out.print("*");
System.out.println();
}
// Lower part
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n-i+1; j++) System.out.print("*");
for(int j = 1; j <= 2*i-2; j++) System.out.print(" ");
for(int j = 1; j <= n-i+1; j++) System.out.print("*");
System.out.println();
}
}
}
Output:
* *
** **
*** ***
**** ****
***** *****
************
************
***** *****
**** ****
*** ***
** **
* *
20. Mirror Image Triangle Pattern
This pattern prints numbers in a triangular format, where the numbers start from 1 and increase sequentially. The design mirrors itself across the center.
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
// Upper part
for(int i = 1; i <= n; i++) {
for(int j = 1; j < i; j++) System.out.print(" ");
for(int j = i; j <= n; j++) System.out.print(j + " ");
System.out.println();
}
// Bottom part
for(int i = 1; i < n; i++) {
for(int j = 1; j <= n-i-1; j++) System.out.print(" ");
for(int j = n-i; j <= n; j++) System.out.print(j + " ");
System.out.println();
}
}
}
Output:
1 2 3 4 5 6
2 3 4 5 6
3 4 5 6
4 5 6
5 6
6
5 6
4 5 6
3 4 5 6
2 3 4 5 6
1 2 3 4 5 6
21. Hollow Triangle Pattern
This pattern creates a hollow triangle, where the border is filled with stars and the inside is empty. It’s a great example of how to print hollow shapes using Java.
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n-i; j++) System.out.print(" ");
for(int j = 1; j <= 2*i-1; j++) {
if(j == 1 || i == n || j == 2*i-1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Output:
*
* *
* *
* *
* *
***********
22. Hollow Reverse Triangle Pattern
This pattern is the reverse of the Hollow Triangle Pattern. It starts with a wide base and narrows to a single star at the top.
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
for(int i = 1; i <= n; i++) {
for(int j = 1; j < i; j++) System.out.print(" ");
for(int j = 1; j <= 2*(n-i+1)-1; j++) {
if(j == 1 || i == 1 || j == 2*(n-i+1)-1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Output:
***********
* *
* *
* *
* *
*
23. Hollow Diamond Pyramid
This pattern combines both the upper and lower halves of a diamond shape, with stars filling the borders and spaces in the center.
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
// Upper half
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n-i; j++) System.out.print(" ");
for(int j = 1; j <= 2*i-1; j++) {
if(j == 1 || j == 2*i-1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
// Lower half
for(int i = 1; i < n; i++) {
for(int j = 1; j <= i; j++) System.out.print(" ");
for(int j = 1; j <= (n-i)*2-1; j++) {
if(j == 1 || j == (n-i)*2-1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Output:
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
24. Hollow Hourglass Pattern
This pattern creates an hourglass shape, with stars at the borders and spaces in between. The pattern mirrors itself at the center.
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//Square hallow pattern
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
/*
i = 1, space = 0, n = 5, clen = 5
i = 2, space = 1, n = 5, clen = 4
i = 3, space = 2, n = 5, clen = 3
i = 4, space = 3, n = 5, clen = 2
i = 5, space = 4, n = 5, clen = 1
*/
//upper part of the pattern
for(int i=1;i<=n;i++){
//Print leading spaces
for(int j=1;j<i;j++){
System.out.print(" ");
}
//Print stars and spaces in side the hallow upper triangle
for(int j=1;j<=n-i+1;j++){
if(j==1 || i==1 || j == n-i+1){
System.out.print("*" + " ");
}else{
System.out.print(" "); //double space
}
}
System.out.println();
}
/*
i = 1, space = 3, n = 5 , clen = 2
i = 2, space = 2, n = 5 , clen = 3
i = 3, space = 1, n = 5 , clen = 4
i = 4, space = 0, n = 5 , clen = 5
*/
//Lower part of the pattern
for(int i=1;i<n;i++){
//Print leading spaces
for(int j=1;j<=n-i-1;j++){
System.out.print(" ");
}
//Print the stars and the spaces in the hallow triangle
for(int j=1;j<=i+1;j++){
if(j==1 || i == n-1 || j==i+1){
System.out.print("*"+" ");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
Output:
* * * * * *
* *
* *
* *
* *
*
* *
* *
* *
* *
* * * * * *
25. X — Star Pattern
The pattern is divided into three parts: the upper half (above the middle), the middle row, and the lower half (below the middle).
Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the input: ");
int n = sc.nextInt();
/*
i=1, spaces = 0, n = 5, winLen = 5
i=2, spaces = 1, n = 5 ,winLen = 4
i=3, spaces = 2, n = 5 ,winLen = 3
i=4, spaces = 3, n = 5 ,winLen = 2
i=5, spaces = 4, n = 5 ,winLen = 1
*/
//Upper pattern
//outer loop for rows
for(int i=1;i<=n;i++){
//Print leading spaces
for(int j=1;j<=i-1;j++){
System.out.print(" ");
}
//Print star and hallow spaces
for(int j=1;j<=n-i+1;j++){
if(j==1 || j==n-i+1){
System.out.print("* ");
}else {
System.out.print(" ");
}
}
System.out.println();
}
/*
i=1, spaces = 3, n = 5, winLen = 2
i=2, spaces = 2, n = 5 ,winLen = 3
i=3, spaces = 1, n = 5 ,winLen = 4
i=4, spaces = 0, n = 5 ,winLen = 5
*/
//Lower pattern
//Outer loop for rows
for(int i=1;i<n;i++){
//Print spaces
for(int j=1;j<=n-i-1;j++){
System.out.print(" ");
}
//Print stars and hallow spaces
for(int j=1;j<=i+1;j++){
if(j==1 || j==i+1){
System.out.print("* ");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
Output:
* *
* *
* *
* *
*
* *
* *
* *
* *
These 25 Java pattern printing examples provide a solid foundation for practicing your coding skills. By understanding and implementing these patterns, you’ll improve your problem-solving abilities and become more proficient in using loops and conditionals. Happy coding!