Pattern Programs in Java: Code Examples and Solutions
TL;DR: Java pattern programs use nested loops to print shapes made of stars, numbers, or letters, and they're a staple of both beginner practice and coding interviews. This guide covers 26 patterns, including triangles, pyramids, diamonds, hollow shapes, Pascal's and Floyd's triangles, and a butterfly pattern. Every code sample below has been compiled and run to verify it produces the output shown. If you're short on time, jump straight to the most commonly asked interview patterns.

Java is one of the most popular programming languages, valued for its simplicity and versatility across web, mobile, and desktop development. Java pattern programs, where you print numbers, stars, or letters in a specific shape, are one of the best ways for beginners to learn Java, since they force you to work directly with loops, conditionals, and logic rather than just memorizing syntax.

Pattern programs also recur in Java interviews and can look deceptively difficult, even though they're built on simple row-and-column logic. This guide covers 26 Java pattern programs, from basic triangles to Pascal's triangle and hollow shapes, each with working code, verified output, and a short explanation of the logic behind it.

How to Print a Pattern in Java?

Printing patterns in Java is a common task in programming, especially for beginners. Patterns are printed by arranging symbols or numbers in a specific way to form a design or shape. These patterns are often used in problem-solving and can help develop algorithmic thinking skills.

Loops and control statements are best for printing patterns in Java. Loops repeat a block of code until a condition is met, and control statements let you change the program flow based on certain conditions. The different pattern problems in Java are discussed below.

Java Certification TrainingENROLL NOW
Boost Your Career and Unlock High-Paying Jobs!

How Do Nested Loops Work in Java Pattern Programs?

Almost every pattern program uses two loops, one nested inside the other, and understanding what each one does makes every pattern on this page easier to read:

  • The outer loop controls the rows. It runs once per line of the shape, so if a pattern has 5 rows, the outer loop runs 5 times.
  • The inner loop (or loops) controls what gets printed within a single row, character by character, whether that's stars, spaces, numbers, or letters. Many patterns use two inner loops per row, one to print leading spaces for alignment, and a second to print the actual symbols.
  • A System.out.println() after the inner loop(s) finish is what moves the cursor to the next row. Forgetting it is one of the most common pattern-program bugs, since it silently prints the whole shape on a single line instead of stacking it row by row.

Once you can identify which loop is doing which job, you can adapt almost any pattern below to a different size, symbol, or orientation.

Which Loop Is Best for Java Pattern Programs?

for loops are the standard choice for Java pattern programs, and every example on this page uses one, because a pattern's row and column counts are almost always known before the loop starts. A for loop keeps the initialization, condition, and increment together in one line, which makes the row/column logic easier to read at a glance. A while loop can produce the same output; it just spreads that same logic across more lines. Here's the first pattern below written both ways to show the difference:

With a for loop:

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

With a while loop:

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

Both produce identical output. Do-while loops are rarely used for patterns because they always execute at least once, even when the row or column count is zero, which isn't the behavior most patterns need.

Java Certification TrainingENROLL NOW
Take the Leap and Master Java Development

26 Java Pattern Programs

Star Patterns in Java

Star patterns are a popular Java pattern program, often used to create interesting visual designs or graphics. These programs use asterisks or other symbols to create various shapes and patterns. Star patterns are commonly used in computer graphics, logo designs, and other visual displays.

Creating star patterns in Java involves using nested loops to control the number of rows and columns and the position of the asterisks or other symbols. You can customize the program to create patterns such as triangles, squares, circles, and more complex designs.

Pattern 1: Left-Aligned Star Triangle

package Patterns;

public class Star {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; ++i) { //Outer loop for rows
            for (int j = 1; j <= i; ++j) { //Inner loop for Col
                System.out.print("* "); //Print *
            }
            System.out.println(); //New line
        }
    }
}

Output:

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

Pattern 2: Reverse Star Triangle

package Patterns;

public class Star {
    public static void main(String[] args) {
        int rows = 5;
        for(int i = rows; i >= 1; --i) { //For Loop for Row
            for(int j = 1; j <= i; ++j) { //For Loop for Col
                System.out.print("* "); //Prints *
            }
            System.out.println(); //Get to newline
        }
    }
}

Output:

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

Pattern 3: Full Pyramid and Reverse Pyramid Combined

Takes a row count as input and prints a growing triangle immediately followed by a shrinking one.

package Patterns;

import java.util.Scanner;

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

        for (int i = 0; i <= rows - 1; i++) { //For Loop for Row
            for (int j = 0; j <= i; j++) { //For Loop for Col
                System.out.print("*" + " "); //prints * and blank space
            }
            System.out.println(""); // new line
        }

        for (int i = rows - 1; i >= 0; i--) { //For Loop for Row
            for (int j = 0; j <= i - 1; j++) { //For Loop for Col
                System.out.print("*" + " "); //prints * and blank space
            }
            System.out.println("");// new line
        }
        sc.close();
    }
}

Output (for rows = 5):

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

AI-Powered Full Stack Developer ProgramEXPLORE COURSE
Become a Job-Ready Full-Stack Developer

Pattern 4: Centered Pyramid Pattern

package Patterns;

public class Star {
    public static void printStars(int n) {
        int i, j;
        for (i = 0; i < n; i++) {
            for (j = 2 * (n - i); j >= 0; j--) { //For Loop for Row
                System.out.print(" "); // Print Spaces
            }
            for (j = 0; j <= i; j++) { //For Loop for col
                System.out.print("* "); // Print Star
            }
            System.out.println();
        }
    }
    public static void main(String args[]) {
        int n = 5; //Number of Rows
        printStars(n);
    }
}

Output:

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

Pattern 5: Centered Reverse Pyramid Pattern

package Patterns;

import java.util.Scanner;

public class Star {
    public static void main(String[] args) {
        Scanner S = new Scanner(System.in); //Input
        System.out.println("Enter row value ");
        int r = S.nextInt();

        for (int i = r; i >= 1; i--) {
            for (int j = r; j > i; j--) {
                System.out.print(" "); // Prints Blank space
            }
            for (int k = 1; k <= i; k++) {
                System.out.print("*"); //Prints *
            }
            System.out.println(" "); //Prints blank spaces
        }
        S.close();
    }
}

Output (for r = 5):

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

Pattern 6: Diamond Outline Pattern (Kite Shape)

package Patterns;

import java.util.Scanner;

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

        for (int i = 1; i <= rows; i++) {
            for (int j = i; j < rows; j++) { //Rows Loop
                System.out.print(" "); // Blank Space
            }
            for (int k = 1; k <= i; k++) { //Cols Loop
                System.out.print("*"); // Prints *
            }
            System.out.println("");
        }
        for (int i = rows; i >= 1; i--) {
            for (int j = i; j <= rows; j++) { //Rows Loop
                System.out.print(" "); // Prints blank spaces
            }
            for (int k = 1; k < i; k++) { //Col Loop
                System.out.print("*"); // Prints *
            }
            System.out.println(""); // New Line
        }
        sc.close();
    }
}

Output (for rows = 5):

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

Pattern 7: Right-Angled Triangle Pattern

package Patterns;

public class Star {
    public static void printTriagle(int n) {
        for (int i = 0; i < n; i++) {
            for (int j = n - i; j > 1; j--) { //Loop for blank space
                System.out.print(" "); //Print Space
            }
            for (int j = 0; j <= i; j++) { //Loop for star
                System.out.print("* "); //Print Star
            }
            System.out.println(); //Newline
        }
    }
    public static void main(String args[]) {
        int n = 5;
        printTriagle(n);
    }
}

Output:

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

AI-Powered Full Stack Developer ProgramEXPLORE COURSE
Advance Your Full Stack Career!

Pattern 8: Reverse Right-Angled Triangle Pattern

package Patterns;

import java.util.Scanner;

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

        for (int i = 0; i <= rows - 1; i++) { //For loop for Rows
            for (int j = 0; j <= i; j++) { //For loop for Col
                System.out.print(" "); // blank space
            }
            for (int k = 0; k <= rows - 1 - i; k++) {
                System.out.print("*" + " "); // prints * and blank space
            }
            System.out.println(); //Next line
        }
        sc.close();
    }
}

Output (for rows = 5):

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

Pattern 9: Diamond Star Pattern

<a id="pattern-9"></a>

A classic interview favorite: a solid, centered diamond made of stars.

package Patterns;

import java.util.Scanner;

public class Star {
    public static void main(String args[]) {
        int n, x, j, blank = 1;
        System.out.print("Enter the value for rows: ");
        Scanner s = new Scanner(System.in);
        n = s.nextInt(); //input
        blank = n - 1; // logic for blank spaces

        //Upper star Pyramid
        for (j = 1; j <= n; j++) {
            for (x = 1; x <= blank; x++) {
                System.out.print(" "); //print blank space
            }
            blank--;
            for (x = 1; x <= 2 * j - 1; x++) {
                System.out.print("*"); //Print Star
            }
            System.out.println("");
        }

        //Lower star Pyramid
        blank = 1;
        for (j = 1; j <= n - 1; j++) {
            for (x = 1; x <= blank; x++) {
                System.out.print(" "); //Print Spaces
            }
            blank++;
            for (x = 1; x <= 2 * (n - j) - 1; x++) {
                System.out.print("*"); //Print Star
            }
            System.out.println(""); //Print new line
        }
    }
}

Output (for n = 5):

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
If you are a fresh graduate, self-taught developer, or career switcher with basic programming knowledge, Java can be a practical way into software development. Simplilearn's Java Developer roadmap shows how to turn that starting point into a structured career path.

Pattern 10: Hourglass (Sandglass) Star Pattern

package Patterns;

import java.util.Scanner;

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

        //Upper Inverted Pyramid
        for (int i = 0; i <= rows - 1; i++) {
            for (int j = 0; j < i; j++) {
                System.out.print(" "); //Print blank space
            }
            for (int k = i; k <= rows - 1; k++) {
                System.out.print("*" + " "); //Print star and blank space
            }
            System.out.println(""); //New line
        }

        //For lower Pyramid
        for (int i = rows - 1; i >= 0; i--) {
            for (int j = 0; j < i; j++) {
                System.out.print(" "); //Print spaces
            }
            for (int k = i; k <= rows - 1; k++) {
                System.out.print("*" + " "); //Print Star and Space
            }
            System.out.println(""); //Print New line
        }
        sc.close();
    }
}

Output (for rows = 5):

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

With Our Trending Applied Agentic AI CourseExplore Course
Learn to Build Cutting-edge Agentic AI Products

Pattern 11: Diagonal Star Pattern

The original version of this program was missing a System.out.println() inside the row loop, so it printed every star on a single line instead of the intended diagonal. That's fixed below.

public class Star {
    public static void main(String[] args) {
        int i, j;
        for (i = 1; i <= 5; i++) {
            for (j = 0; j < 5 - i; j++) {
                System.out.print(" "); //Print blank space
            }
            System.out.println("*"); //Print Star and move to a new line
        }
    }
}

Output:

    *
   *
  *
 *
*

X-Pattern and V-Shape Patterns

These patterns use conditional logic inside the inner loop to print stars only at specific positions, forming a diagonal, letter, or symmetric shape instead of a filled triangle.

Pattern 12: X Star Pattern

package Patterns;

import java.util.*;

public class Star {
    public static void main(String args[]) {
        int i, j, n;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a value for n");
        n = sc.nextInt(); //Input

        //Upper V pattern
        for (i = n; i >= 1; i--) {
            for (j = i; j < n; j++) {
                System.out.print(" ");//print spaces
            }
            for (j = 1; j <= (2 * i - 1); j++) {
                if (j == 1 || j == (2 * i - 1))//Logic for printing star
                    System.out.print("*");
                else
                    System.out.print(" ");//if logic fails print space
            }
            System.out.println("");
        }

        //Lower Inverted V pattern
        for (i = 2; i <= n; i++) {
            for (j = i; j < n; j++) {
                System.out.print(" ");//Print spaces
            }
            for (j = 1; j <= (2 * i - 1); j++) {
                if (j == 1 || j == (2 * i - 1))//Logic for printing star
                    System.out.print("*");
                else
                    System.out.print(" ");//if logic fails print space
            }
            System.out.println("");
        }
    }
}

Output (for n = 5):

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

Pattern 13: Inverted V Star Pattern

package Patterns;

import java.util.Scanner;

public class Star {
    public static void main(String[] args) {
        Scanner cs = new Scanner(System.in); //Input
        System.out.println("Enter the row size:");
        int out, in;
        int row_size = cs.nextInt();
        int print_control_x = row_size;
        int print_control_y = row_size;

        for (out = 1; out <= row_size; out++) {
            for (in = 1; in <= row_size * 2; in++) {
                if (in == print_control_x || in == print_control_y) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            print_control_x--;
            print_control_y++;
            System.out.println();
        }
        cs.close();
    }
}

Output (for row_size = 5):

    *     
   * *    
  *   *   
 *     *  
*       *

Pattern 14: V Star Pattern (Converging)

The original version used System.out.print(""), which prints an empty string and does nothing, instead of System.out.println(), so it never actually moved to a new line. That's fixed below.

package Patterns;

public class Star {
    static void pattern(int n) {
        int i, j;
        for (i = n - 1; i >= 0; i--) {
            for (j = n - 1; j > i; j--) {
                System.out.print(" "); //Print Space
            }
            System.out.print("*"); //Print star
            for (j = 1; j < (i * 2); j++)
                System.out.print(" ");//Print space
            if (i >= 1)
                System.out.print("*");
            System.out.println(); //Move to a new line
        }
    }
    public static void main(String args[]) {
        int n = 5;
        pattern(n); //Pattern method call
    }
}

Output:

*       *
 *     *
  *   *
   * *
    *

Pattern 15: Rhombus (Hollow Diamond) Pattern

The comments in this program were originally missing a second forward slash (/Logic for pattern instead of //Logic for pattern), which is a syntax error that would stop the code from compiling. Both instances are fixed below.

package Patterns;

import java.util.Scanner;

public class Star {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Number of rows: ");
        int rows = sc.nextInt();
        int i;

        //upper inverted V pattern
        for (i = 1; i <= rows; i++) {
            for (int j = rows; j > i; j--) {
                System.out.print(" "); //Print spaces
            }
            System.out.print("*"); //Print Star
            for (int k = 1; k < 2 * (i - 1); k++) { //Logic for pattern
                System.out.print(" "); //Print Spaces
            }
            if (i == 1) {
                System.out.println(""); //Condition true, go to newline
            } else {
                System.out.println("*"); //else print star
            }
        }

        //Lower v pattern
        for (i = rows - 1; i >= 1; i--) {
            for (int j = rows; j > i; j--) {
                System.out.print(" "); //Print Spaces
            }
            System.out.print("*");
            for (int k = 1; k < 2 * (i - 1); k++) { //Logic for pattern
                System.out.print(" ");
            }
            if (i == 1)
                System.out.println(""); //newline
            else
                System.out.println("*"); //Print star
        }
        sc.close();
    }
}

Output (for rows = 5):

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

Hollow Patterns

Hollow patterns use the same row/column loop structure as solid shapes, but add one extra condition: print the symbol only at the boundary of each row (the first position, the last position, or the first/last row overall), and print a space everywhere else.

Pattern 16: Hollow Triangle Pattern

package Patterns;

import java.util.Scanner;

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

        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++) {
                if (k == 1 || i == rows || k == (2 * i - 1)) {
                    //Logic for printing Pattern
                    System.out.print("*"); //Print Star
                } else {
                    System.out.print(" "); //Print Spaces
                }
            }
            System.out.println("");
        }
        sc.close();
    }
}

Output (for rows = 5):

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

Java Certification TrainingENROLL NOW
Master Core Java 8 Concepts, Java Servlet, & More!

Pattern 17: Inverted Hollow Triangle Pattern

package Patterns;

import java.util.Scanner;

public class Star {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of rows: ");
        int rows = sc.nextInt(); //Row input

        for (int i = rows; i >= 1; i--) {
            for (int j = i; j < rows; j++) {
                System.out.print(" "); //Print Spaces
            }
            for (int k = 1; k <= (2 * i - 1); k++) {
                if (k == 1 || i == rows || k == (2 * i - 1)) { //logic to print Pattern
                    System.out.print("*"); //True prints star
                } else {
                    System.out.print(" "); //False prints spaces
                }
            }
            System.out.println("");
        }
        sc.close();
    }
}

Output (for rows = 5):

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

Pattern 18: Hollow Square (Box) Pattern

package Patterns;

public class Star {
    static void print_rectangle(int n, int m) {
        int i, j;
        for (i = 1; i <= n; i++) {
            for (j = 1; j <= m; j++) {
                if (i == 1 || i == n || j == 1 || j == m) //Logic to print
                    System.out.print("*"); //True, print star
                else
                    System.out.print(" "); //False, print space
            }
            System.out.println();
        }
    }
    public static void main(String args[]) {
        int rows = 10, columns = 10;
        print_rectangle(rows, columns); //Method call
    }
}

Output:

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

Pattern 19: Butterfly Star Pattern (New)

A symmetric shape with two solid triangles that mirror each other and meet in the middle. This is one of the most frequently searched star patterns and previously wasn't covered on this page.

package Patterns;

public class Butterfly {
    public static void printButterfly(int n) {
        // Upper half
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*"); // Left wing
            }
            for (int j = 1; j <= 2 * (n - i); j++) {
                System.out.print(" "); // Gap between wings
            }
            for (int j = 1; j <= i; j++) {
                System.out.print("*"); // Right wing
            }
            System.out.println();
        }
        // Lower half (mirrors the upper half)
        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            for (int j = 1; j <= 2 * (n - i); j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
    public static void main(String[] args) {
        int n = 5;
        printButterfly(n);
    }
}

Output (for n = 5):

*        *
**      **
***    ***
****  ****
**********
**********
****  ****
***    ***
**      **
*        *
Learn 45+ in-demand full-stack development skills and tools, including Frontend Development, Backend Development, Version Control and Collaboration, Database Management, and AI-Assisted Development, with our AI-Powered Full Stack Developer Course.

Number Triangle Pattern

<a id="pattern-19"></a>

Pattern 20: Number Triangle Pattern

This is one of the most common Java pattern programs for beginners.

package Patterns;

import java.util.Scanner;

public class Star {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in); //Input
        System.out.println("Number of rows: ");
        int rows = sc.nextInt();

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + " "); //Print j value and space
            }
            System.out.println();
        }
        sc.close();
    }
}

Output (for rows = 5):

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5

Floyd's Triangle Pattern

<a id="pattern-20"></a>

This pattern prints a triangle of numbers, starting at 1 and incrementing by 1 for each row, without ever resetting.

Pattern 21: Floyd's Triangle Pattern

The original comment here was also missing a second forward slash (/Floyd's triangle logic), which is a syntax error—fixed below.

package Patterns;

public class Star {
    public static void main(String[] args) {
        int i, j, k = 1;
        for (i = 1; i <= 5; i++) {
            for (j = 1; j < i + 1; j++) {
                System.out.print(k++ + " "); //Floyd's triangle logic (prints k, then increments it)
            }
            System.out.println();
        }
    }
}

Output:

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

Pascal's Triangle

<a id="pattern-21"></a>

It involves creating a triangular pattern of numbers that follow a specific mathematical rule. You can create this pattern with a nested loop that calculates each number based on its position in the triangle.

Java Certification TrainingENROLL NOW
Dive Deep Into Java Core Concepts

Pattern 22: Pascal's Triangle Pattern

package Patterns;

public class Star {
    public static void main(String[] args) {
        int x = 6;
        for (int i = 0; i < x; i++) {
            int num = 1;
            System.out.printf("%" + (x - i) * 2 + "s", ""); //Pascal's triangle logic
            for (int j = 0; j <= i; j++) {
                System.out.printf("%4d", num);
                num = num * (i - j) / (j + 1);
            }
            System.out.println();
        }
    }
}

Output:

               1
             1   1
           1   2   1
         1   3   3   1
       1   4   6   4   1
     1   5  10  10   5   1

Numeric Patterns

This is another type of Java pattern program that prints numbers in a specific sequence or arrangement. These programs can create tables, graphs, and other visual displays.

In Java, creating numeric patterns involves using loops to control the number of rows and columns and the values printed. You can customize the program to create patterns such as multiplication tables, Fibonacci sequences, and more complex designs.

Pattern 23: Binary (Zero-One) Triangle Pattern

package Patterns;

import java.util.Scanner;

public class Star {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Number of rows: ");
        int rows = sc.nextInt(); //Input

        for (int i = 1; i <= rows; i++) {
            int num;
            if (i % 2 == 0) {
                num = 0;
                for (int j = 1; j <= rows; j++) {
                    System.out.print(num);
                    num = (num == 0) ? 1 : 0;
                }
            } else {
                num = 1;
                for (int j = 1; j <= rows; j++) {
                    System.out.print(num);
                    num = (num == 0) ? 1 : 0;
                }
            }
            System.out.println();
        }
        sc.close();
    }
}

Output (for rows = 5):

10101
01010
10101
01010
10101

Pattern 24: Alphabet Pyramid (Ruby) Pattern

Prints a pyramid of a chosen letter, mirrored on either side, based on that letter's position in the alphabet.

package Patterns;

import java.util.Scanner;

public class Ruby {
    public static void main(String[] args) {
        char[] alpha = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
                'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
        int digit = 0;
        String[] ruby = new String[26];
        System.out.print("Input Uppercase alphabet between A to Z:");
        Scanner reader = new Scanner(System.in);
        try {
            char alphabet = reader.next("[A-Z]").charAt(0);
            for (int i = 0; i < alpha.length; i++) {
                if (alpha[i] == alphabet) {
                    digit = i;
                    break;
                }
            }
            for (int i = 0; i <= digit; i++) {
                ruby[i] = "";
                for (int p = 0; p < digit - i; p++) {
                    ruby[i] += " ";
                }
                ruby[i] += alpha[i];
                if (alpha[i] != 'A') {
                    for (int p = 0; p < 2 * i - 1; p++) {
                        ruby[i] += " ";
                    }
                    ruby[i] += alpha[i];
                }
                System.out.println(ruby[i]);
            }
            for (int i = digit - 1; i >= 0; i--) {
                System.out.println(ruby[i]);
            }
        } catch (Exception e) { //Exception
            e.printStackTrace();
        } finally {
            reader.close();
        }
    }
}

Output (for input D):

   A
  B B
 C   C
D     D
 C   C
  B B
   A

AI-Powered Full Stack Developer ProgramExplore Program
Get the Coding Skills You Need to Succeed

Pattern 25: Alphabet Triangle Pattern (Left-Aligned)

package Patterns;

public class Alphabet {
    public static void main(String[] args) {
        int alphabet = 65; //ASCII value of "A"
        for (int i = 0; i <= 5; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print((char) (alphabet + j) + " "); //Logic to print Alphabet pattern
            }
            System.out.println();
        }
    }
}

Output:

A 
A B 
A B C 
A B C D 
A B C D E 
A B C D E F

Pattern 26: Alphabet Triangle Pattern (Right-Aligned)

package Patterns;

public class Alphabet {
    public static void main(String[] args) {
        for (int i = 0; i <= 4; i++) {
            int alphabet = 65; //ASCII value of "A"
            for (int j = 4; j > i; j--) {
                System.out.print(" "); //Print Space
            }
            for (int k = 0; k <= i; k++) {
                System.out.print((char) (alphabet + k) + " "); //Print Alphabet
            }
            System.out.println();
        }
    }
}

Output:

    A 
   A B 
  A B C 
 A B C D 
A B C D E

Conclusion

Java pattern programs help enhance a Java developer's analytical capabilities. Practicing them will not only help you crack interviews but also help you decode the critical, logical issues in your code.

After practicing Java Pattern Programs, you can also review a tutorial on some of the most frequently asked Java interview questions so you can be interview-ready at all times.

If you want deeper knowledge of Java and want to get certified as a professional developer, explore our Java training and certification programs. Simplilearn's qualified industry experts offer the same in real-time experience. Check out our Java Certification Training today!

Once you're comfortable with Java and core programming concepts, you can also explore AI-assisted and agentic development. AI agents help developers automate tasks and manage complex workflows. If you're looking to build these skills, Simplilearn's Agentic AI Certification provides live training focused on building and managing autonomous AI solutions using tools and frameworks including LangChain, AutoGen, CrewAI, n8n, Miro, Lovable, and Figma.

Key Takeaways

  • Every pattern program is built from two nested loops: the outer loop controls rows, the inner loop(s) control what's printed within a row, and a println() after them moves to the next line.
  • for loops are the standard choice for pattern programs since row and column counts are known in advance, though while loops produce identical output for the same logic.
  • Hollow patterns use the same structure as solid ones, with one added condition: print the symbol only at the row or column boundaries, and a space everywhere else.
  • The star pyramid, diamond, Pascal's triangle, Floyd's triangle, and number triangle patterns are the ones most commonly asked in interviews.
  • All 26 patterns in this guide were compiled and run to confirm the code and output match.

FAQs

1. What is the time complexity of pattern programs in Java?

Most Java pattern programs run in O(n²) time, since they use two nested loops, one for rows and one for columns, even when the amount actually printed per row varies. Space complexity is typically O(1), since the code only needs a few loop counters and no extra data structures.

2. Do you need Scanner input for pattern programs, or can the row count be hardcoded?

Either works. Hardcoding the row count keeps the code focused purely on the pattern logic and is easier to test, which is why several examples above use it. Reading the row count with Scanner, as most of the patterns above do, makes the same program reusable for any size without editing the code.

3. Can Java pattern programs be written using recursion instead of loops?

Yes, though it's uncommon. Loops are almost always the more natural fit since a pattern's row and column counts are known upfront. Still, a recursive version can replace the outer loop with a method that calls itself for the next row until it hits a base case, typically when the remaining row count reaches 0.

4. What are common mistakes when writing Java pattern programs?

The most frequent ones are forgetting a println() after each row, which prints the whole shape on a single line instead of stacking it; using print("") instead of println() by mistake, which does nothing at all; leaving a stray single slash in a comment, which is a compile error rather than just a formatting issue; and getting a space-padding loop's start or end condition off by one, which throws off the alignment of the entire shape.

About the Author

Ravikiran A SRavikiran A S

Ravikiran A S is a Technical Content Strategist and Data Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

View More
  • Acknowledgement
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.
  • *All trademarks are the property of their respective owners and their inclusion does not imply endorsement or affiliation.
  • Career Impact Results vary based on experience and numerous factors.