Pattern Programs in Java : Top 10 pattern based programs

Overview

Pattern programs are a great way to improve your coding skills, especially when you’re learning a new programming language like Java . They help you understand loops, nested loops, and logic building. In this article, we’ll walk you through some common Java pattern programs step by step. Don’t worry if you’re a beginner—we’ll keep it simple and easy to follow!

Java Pattern Programs

What are Pattern Programs?

Pattern programs are exercises where you print specific shapes or designs using characters like stars (*), numbers, or letters. These patterns are created using loops

Let’s start with some basic patterns and gradually move to more complex ones.

1. Right-Angled Triangle Pattern

Example


*
**
***
****
*****

Steps to Create:

  • Use a loop to control the rows.
  • Use another loop inside the first loop to print the stars (*) for each row.

public class RightAngledTriangle {
    public static void main(String[] args) {
        int rows = 5; // Number of rows in the pattern

        for (int i = 1; i <= rows; i++) { // Loop for rows
            for (int j = 1; j <= i; j++) { // Loop for columns
                System.out.print("*");
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

2. Inverted Right-Angled Triangle Pattern

Example


*****
****
***
**
*

Steps to Create:

  • Use a loop to control the rows, but start from the maximum number of stars and decrease.
  • Use another loop to print the stars for each row.

public class InvertedRightAngledTriangle {
    public static void main(String[] args) {
        int rows = 5; // Number of rows in the pattern

        for (int i = rows; i >= 1; i--) { // Loop for rows (start from the top)
            for (int j = 1; j <= i; j++) { // Loop for columns
                System.out.print("*");
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

3. Pyramid Pattern

Example


    *
   ***
  *****
 *******
*********

Steps to Create:

  • Use a loop to control the rows.
  • Use two inner loops: one for printing spaces and another for printing stars.
  • The number of spaces decreases as you move down the rows, while the number of stars increases.

public class PyramidPattern {
    public static void main(String[] args) {
        int rows = 5; // Number of rows in the pattern

        for (int i = 1; i <= rows; i++) { // Loop for rows
            // Print spaces
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" ");
            }
            // Print stars
            for (int k = 1; k <= 2 * i - 1; k++) {
                System.out.print("*");
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

4. Diamond Pattern

Example


    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Steps to Create:

  • The diamond pattern is a combination of a pyramid and an inverted pyramid.
  • First, print the pyramid pattern.
  • Then, print the inverted pyramid pattern below it.

public class DiamondPattern {
    public static void main(String[] args) {
        int rows = 5; // Number of rows in the pattern

        // Upper part of the diamond (pyramid)
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= 2 * i - 1; k++) {
                System.out.print("*");
            }
            System.out.println();
        }

        // Lower part of the diamond (inverted pyramid)
        for (int i = rows - 1; i >= 1; i--) {
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= 2 * i - 1; k++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

5. Number Pattern

Example


1
12
123
1234
12345

Steps to Create:

  • Use a loop to control the rows.
  • Use another loop to print numbers in increasing order for each row.

public class NumberPattern {
    public static void main(String[] args) {
        int rows = 5; // Number of rows in the pattern

        for (int i = 1; i <= rows; i++) { // Loop for rows
            for (int j = 1; j <= i; j++) { // Loop for columns
                System.out.print(j); // Print numbers
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

6. Hollow Pyramid Pattern

Example


    *
   * *
  *   *
 *     *
*********

Steps to Create:

  • Print spaces before the stars to align the pyramid.
  • Print stars only at the beginning and end of each row.
  • For the last row, print a complete line of stars.

public class HollowPyramid {
    public static void main(String[] args) {
        int rows = 5; // Number of rows in the pattern

        for (int i = 1; i <= rows; i++) { // Loop for rows
            // Print spaces
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" ");
            }
            // Print stars and spaces
            for (int k = 1; k <= 2 * i - 1; k++) {
                if (k == 1 || k == 2 * i - 1 || i == rows) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

7. Pascal’s Triangle Pattern

Example


    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1

Steps to Create:

  • Use nested loops to calculate and print the values of Pascal’s Triangle.
  • Each number is the sum of the two numbers directly above it.

public class PascalTriangle {
    public static void main(String[] args) {
        int rows = 5; // Number of rows in the pattern

        for (int i = 0; i < rows; i++) { // Loop for rows
            int number = 1;
            // Print spaces
            for (int j = 0; j < rows - i; j++) {
                System.out.print(" ");
            }
            // Print numbers
            for (int k = 0; k <= i; k++) {
                System.out.print(number + " ");
                number = number * (i - k) / (k + 1); // Calculate the next number
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

8. Floyd’s Triangle Pattern

Example


1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Steps to Create:

  • Use a counter variable to keep track of the numbers.
  • Print numbers in increasing order for each row.

public class FloydTriangle {
    public static void main(String[] args) {
        int rows = 5; // Number of rows in the pattern
        int number = 1; // Counter for numbers

        for (int i = 1; i <= rows; i++) { // Loop for rows
            for (int j = 1; j <= i; j++) { // Loop for columns
                System.out.print(number + " ");
                number++; // Increment the number
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

9. Butterfly Pattern

Example


*        *
**      **
***    ***
****  ****
**********
****  ****
***    ***
**      **
*        *

Steps to Create:

  • Divide the pattern into two parts: upper and lower.
  • For the upper part, print stars in increasing order and spaces in decreasing order.
  • For the lower part, print stars in decreasing order and spaces in increasing order.

public class ButterflyPattern {
    public static void main(String[] args) {
        int rows = 5; // Number of rows in the pattern

        // Upper part of the butterfly
        for (int i = 1; i <= rows; i++) {
            // Print left stars
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            // Print spaces
            for (int j = 1; j <= 2 * (rows - i); j++) {
                System.out.print(" ");
            }
            // Print right stars
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        // Lower part of the butterfly
        for (int i = rows - 1; i >= 1; i--) {
            // Print left stars
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            // Print spaces
            for (int j = 1; j <= 2 * (rows - i); j++) {
                System.out.print(" ");
            }
            // Print right stars
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

10. Zig-Zag Pattern

Example


  *   *  
 * * * * 
*   *   *

Steps to Create:

  • Use a loop to control the rows.
  • Print stars and spaces in a zig-zag manner.

public class ZigZagPattern {
    public static void main(String[] args) {
        int rows = 3; // Number of rows in the pattern
        int columns = 9; // Number of columns in the pattern

        for (int i = 1; i <= rows; i++) { // Loop for rows
            for (int j = 1; j <= columns; j++) { // Loop for columns
                if ((i + j) % 4 == 0 || (i == 2 && j % 4 == 0)) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println(); // Move to the next line after each row
        }
    }
}

Why Are These Patterns Important in Interviews?

  • Problem-Solving Skills : These patterns test your ability to break down a problem into smaller steps
  • Loop Mastery: They help you practice nested loops and understand their flow.
  • Logical Thinking :You learn to identify patterns and apply logic to print them.
  • Attention to Detail :Small mistakes can ruin the pattern, so you need to be precise.

Conclusion

Pattern programs are a great way to strengthen your Java skills and prepare for interviews. Start with simple patterns and gradually move to more complex ones. With consistent practice, you'll be able to solve any pattern problem with ease!

What are the Pattern Programs in Java

Pattern programs are exercises where you print specific shapes or designs using characters like stars (*), numbers, or letters. These patterns are created using loops

let's explore top 10 java pattern based questions which is most frequently asked in Interview

Top 10 Java pattern based questions

1. Right-Angled Triangle Pattern
2. Inverted Right-Angled Triangle Pattern
3. Pyramid Pattern
4. Diamond Pattern
5. Number Pattern
6. Hollow Pyramid Pattern
7. Pascal's Triangle Pattern
8. Floyd's Triangle Pattern
9. Butterfly Pattern
10. Zig-Zag Pattern

Leave a Comment