Pattern Program in Java

Pattern Program in Java (Different Patterns to Practice)

June 20th, 2026
8073
10:00 Minutes

Want to become a Java Developer but don’t know where to start? Start with pattern programs in Java, where you practice printing shapes using symbols, numbers or letters. These programs help you understand various core concepts such as loops and logic in an easy, visual way.

Starting with pattern programs can be very beneficial for anyone who is learning the fundamentals of programming, particularly when dealing with loops and conditional statements. In this guide, I will explain to you how to create a pattern in Java with real-world examples. All examples in this guide work with Java 8 and above, including the latest LTS versions like Java 17 and Java 21. Let’s begin!

What is a Pattern Program in Java?

The development of pattern programs in Java is an effective means of learning the fundamental elements of programming logic. These programs involve specifically structured pattern printing using letters, numbers or symbols. There are multiple ways to print these patterns, like nested loops, conditional statements and flow control. Practicing pattern programs builds a strong logical foundation that helps learners move toward advanced programming concepts.

How to Print a Pattern in Java: 12 Common Pattern Programs in Java

Now you know what is the importance of this pattern program and why it is useful for beginners who want to start their career as Java developers. Let’s explore some of the most important pattern programs in the Java programming language:

I. Java Pattern Program for Beginners

A. Java Star Pattern Programs

Star pattern programs help beginners to understand nested loops and how row and column logic work in Java.

  • Right triangle
public class PatternPrinter {

    public static void printRightTriangle(int rows) {
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        printRightTriangle(5);
    }
}

Output:

right triangle pattern program in java

Using User Input (Scanner)

In real-world applications and coding interviews, the number of rows is usually taken from user input instead of being hardcoded. This makes the program dynamic and more practical.

import java.util.Scanner;

public class DynamicPattern {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter number of rows: ");
        int rows = sc.nextInt();

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }

        sc.close();
    }
}
  • Inverted triangle
public class InvertedTriangle {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = rows; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Output:

inverted triangle pattern program in java

B. Square Pattern in Java

Square pattern programs demonstrate how loops behave when the number of rows and columns are equal. These patterns strengthen understanding of fixed size iterations.

  • Solid square (stars/numbers)
public class SolidSquareStar {
    public static void main(String[] args) {
        int size = 5;

        for (int i = 1; i <= size; i++) {
            for (int j = 1; j <= size; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Output:

square pattern program in java

II. Java Pattern Programs for Interview

A. Java Number Pattern Programs

Number pattern programs focus on logical sequencing and nested loops. They help beginners understand how numbers change across rows and columns in Java.

  • Floyd’s Triangle
public class FloydsTriangle {
    public static void main(String[] args) {
        int rows = 4;
        int number = 1;

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(number + " ");
                number++;
            }
            System.out.println();
        }
    }
}

Output:

java number pattern program in java

  • Number triangle
public class NumberTriangle {
    public static void main(String[] args) {
        int rows = 4;

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

Output:

number triangle pattern program in java

B. Pyramid Pattern in Java

Pyramid patterns combine spaces and symbols to form symmetrical shapes. These patterns are commonly used to test understanding of alignment and loop control.

  • Star pyramid
public class StarPyramid {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = 1; i <= rows; i++) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= i; k++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Output:

star pyramid pattern program in java

  • Inverted star pyramid
public class InvertedStarPyramid {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = rows; i >= 1; i--) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= i; k++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Output:

inverted star pyramid pattern program in java

C. Alphabet Pattern Program in Java

Alphabet pattern programs use characters and ASCII values to form structured designs. These patterns improve understanding of character manipulation and nested loops in Java.

  • Alphabet triangle (A, AB, ABC…)
public class AlphabetTriangle {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = 1; i <= rows; i++) {
            char ch = 'A';
            for (int j = 1; j <= i; j++) {
                System.out.print(ch + " ");
                ch++;
            }
            System.out.println();
        }
    }
}

Output:

alphabet triangle pattern program in java

Master Java Programming with Java Training

Boost your coding skills and gain hands-on knowledge in Java.

Explore Now

III. Pattern program in Java using loops

A. Triangle Pattern Program in Java

Triangle patterns are the most basic loop based programs. They clearly demonstrate how loop limits affect the shape and size of patterns.

  • Right triangles
public class RightTrianglePatternExample {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Output:

right triangle pattern program in java

B. Square Pattern in Java

Square pattern programs in Java help beginners to understand how loops work when rows and columns have the same fixed size.

  • Number square
public class NumberSquare {
    public static void main(String[] args) {
        int size = 4;

        for (int i = 1; i <= size; i++) {
            for (int j = 1; j <= size; j++) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

Output:

square number pattern programm in java

IV. Advanced pattern program in Java

A. Java Hollow Pattern Programs

Hollow patterns print symbols only at the borders of a shape. They help learners to practice combining loops with if conditions.

  • Hollow square
public class HollowSquare {
    public static void main(String[] args) {
        int size = 5;

        for (int i = 1; i <= size; i++) {
            for (int j = 1; j <= size; j++) {
                if (i == 1 || i == size || j == 1 || j == size) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}

Output:

java hollow pattern programs

  • Hollow triangle
public class HollowTriangle {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                if (j == 1 || j == i || i == rows) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}

Output:

hollow triangle pattern program in java

Diamond Pattern (Interview Favorite)

public class DiamondPattern {
    public static void main(String[] args) {
        int rows = 4;

        for (int i = 1; i <= rows; i++) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= (2 * i - 1); k++) {
                System.out.print("*");
            }
            System.out.println();
        }

        for (int i = rows - 1; i >= 1; i--) {
            for (int j = rows; j > i; j--) {
                System.out.print(" ");
            }
            for (int k = 1; k <= (2 * i - 1); k++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Become a Job-Ready Web Developer

Learn HTML, CSS, JavaScript, and full-stack skills with hands-on projects.

Explore Now

Where Pattern Programs Are Used in Real Projects

Pattern programs may look simple, but they help developers build important programming skills used in real software development. These programs improve logical thinking, debugging ability and understanding of loop-based operations.

In the software industry, the same concepts used in pattern programs are applied in areas like data formatting, report generation, UI rendering, matrix operations and game development logic. Developers also use similar looping structures while working with grids, tables and structured data processing.

Many companies use pattern-based coding questions during technical screening rounds because they help interviewers evaluate problem-solving ability, loop understanding and coding structure.

  • Used in coding interviews and online assessments
  • Helps build logic for competitive programming
  • Useful for understanding matrix and grid problems
  • Improves debugging and flow control skills
  • Builds a strong foundation for Data Structures and Algorithms

Time and Space Complexity of Pattern Programs

Most pattern programs use nested loops. If the outer loop runs n times and the inner loop also runs up to n times, the overall time complexity becomes O(n²). Since these programs only use a few variables and do not allocate additional memory structures, the space complexity remains O(1).

Why Pattern Programs Are Important for Java Interviews

Pattern programs are commonly asked in entry-level and fresher interviews to test logical thinking and loop control. Interviewers often modify basic triangle or square patterns into pyramid, diamond or hollow variations to evaluate problem-solving ability and understanding of nested loops.

Wrapping Up

In this guide, I have explained pattern programs in Java. I showed how to print stars, numbers and letters using basic loops and conditions. These patterns will help beginners understand logic step by step, think clearly and practice coding. They are a great starting point for learning Java and preparing for coding interviews.

Today, most companies use modern Java versions such as Java 17 and Java 21 for enterprise application development. While pattern programs use basic syntax, practicing them helps beginners become comfortable with the core programming logic still used in modern Java applications.

FAQs on Pattern Program in Java

1. Why should beginners learn pattern programs?

Pattern programs improve logical thinking and make it easier to understand nested loops and conditions.

2. Which concepts are used in Java pattern programs?

Loops, nested loops, conditional statements and basic input/output operations are commonly used in Java pattern programs.

3. Are pattern programs important for Java interviews?

Yes, they are asked in interviews to test your problem solving and logical reasoning skills.

4. What is the easiest pattern to start with?

Star patterns like right triangles or squares are the easiest for beginners to start learning.

Explore Our Trending Articles-

About the Author
Author Nehal Sharma
About the Author

Nehal Sharma is a skilled content writer with expertise in Java, mobile development, and data analytics. She transforms complex data into actionable insights and has experience in business intelligence, data science, and Salesforce. She also simplifies technical concepts into clear, engaging content for learners and professionals.

Drop Us a Query
Fields marked * are mandatory
×

Your Shopping Cart


Your shopping cart is empty.