Introduction

For aspiring Java professionals, Java Pattern Programs are critical to understand and practice as the pattern-based Java programs help analyze programming ability. In this listicle, we have tried to provide pattern programs in Java with detailed explanations. This blog will be a comprehensive guide for how to print patterns in Java. 

Contents at a glance:

  1. Pyramid Pattern Programs in Java
  2. Sandglass Java Star Pattern Program
  3. Right Triangle Star Pattern Programs in Java
  4. Left Triangle Star Pattern Programs in Java
  5. Diamond Pattern Program in Java
  6. Downward Triangle Star Pattern Program in Java
  7. Mirrored Right Triangle Star Pattern Programs in Java
  8. Reversed Pyramid Star Pattern Programs in Java
  9. Right down Mirror Star Pattern Programs in Java
  10. Diamond Number Pattern Programs in Java
  11. Alphabet Pattern Programs in Java
  12. Character Pattern Programs in Java
  13. Square Pattern Programs in Java
  14. Pattern Matching Pattern Programs in Java
  15. String Pattern Programs in Java
  16. Word Pattern Programs in Java
  17. Odd Number Pattern Programs in Java
  18. Pascal’s Triangle Pattern Programs in Java
  19. Java Number Pattern Programs
  20. Square Number Pattern Programs in Java
  21. K Shape Alphabet Pattern Program
  22. Triangle Character Pattern Program in Java
  23. Diamond Pattern in Java
  24. Down triangle star program
  25. Alphabet A Pattern
  26. Pattern Programs in Java
  27. Zeroes & ones Pattern Programs
  28. Binary Number Rectangle Pattern
  29. Descending Numbers Pattern Program
  30. Matrix Pattern programs in java

1. Pyramid Pattern Programs in Java

The pyramid is one of the simple pattern programs in Java that is often asked in interviews.

public class JigSawAcademy

{  

    public static void PyramidPattern(int n) 

    {  

        for (int i=0; i<n; i++) // for number of rows(n) 

{ for (int j=n-i; j>1; j–) // a loop for spaces

            { 

                System.out.print(” “); //to print space

            }  

            for (int j=0; j<=i; j++ ) //for number of columns

            { 

                System.out.print(“* “); //to print star

            } 

            System.out.println(); //end-line after every row

        } 

    } 

    public static void main(String args[]) //driver function, 

    { 

        int n = 5; 

        PyramidPattern(n); 

    } 

}

Output:

    * 

   * * 

  * * * 

 * * * * 

* * * * *

It is one of the easiest pattern printing programs in Java. 

2. Sandglass Java Star Pattern Program

import java.util.Scanner;

public class JigSawAcademy

{

    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 (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(“”);

        }

        sc.close();

    }

}

Output:

* * * * * 

 * * * * 

  * * * 

   * * 

    * 

    * 

   * * 

  * * * 

 * * * * 

* * * * *

3. Right Triangle Star Pattern Programs in Java

public class RightTrianglePattern  

{   

public static void main(String args[])   

{   

// the rows to print  

int i, j, row=6;   

//the outer loop for rows  

for(i=0; i<row; i++)   

{   

//the inner loop for columns  

for(j=0; j<=i; j++)   

{   

//to prints stars   

System.out.print(“* “);   

}   

// print new line 

System.out.println();   

}   

}   

Output:

*

* *

* * *

* * * *

* * * * *

4. Left Triangle Star Pattern Programs in Java

public class JigSawAcademy

    public static void printStars(int n) 

    { 

        int i, j;  

        for(i=0; i<n; i++) //an outer loop for number of rows(n) 

{ for(j=2*(n-i); j>=0; j–) // an inner loop for spaces 

            {           

                System.out.print(” “); // to print space

            } 

            for(j=0; j<=i; j++) // an inner loop for columns

            {       

                System.out.print(“* “); // to print star

            }           

            System.out.println(); // end-line after every row

        } 

    } 

    public static void main(String args[]) 

    { 

        int n = 5; 

        printStars(n); 

    } 

}

Output:

           * 

         * * 

       * * * 

     * * * * 

   * * * * *

5. Diamond Pattern Program in Java

import java.util.Scanner;

public class JigSawAcademy

{

public static void main(String args[])

{

int n, i, j, space = 1;

System.out.print(“Enter the number of rows: “);

Scanner s = new Scanner(System.in);

n = s.nextInt();

space = n – 1;

for (j = 1; j<= n; j++)

{

for (i = 1; i<= space; i++)

{

System.out.print(” “);

}

space–;

for (i = 1; i <= 2 * j – 1; i++)

{

System.out.print(“*”);

}

System.out.println(“”);

}

space = 1;

for (j = 1; j<= n – 1; j++)

{

for (i = 1; i<= space; i++)

{

System.out.print(” “);

}

space++;

for (i = 1; i<= 2 * (n – j) – 1; i++)

{

System.out.print(“*”);

}

System.out.println(“”);

}

}

}

Output:

    *

   ***

  *****

 *******

*********

 *******

  *****

   ***

    *

6. Downward Triangle Star Pattern Program in Java

import java.util.Scanner;

public class JigSawAcademy

{

    public static void main(String[] args)

    {

    Scanner sc = new Scanner(System.in);

    System.out.println(“Enter the number of rows: “); //to take input from user

    int rows = sc.nextInt();

    for (int i= rows-1; i>=0 ; i–)

    {

    for (int j=0; j<=i; j++)

    {

    System.out.print(“*” + ” “);

    }

    System.out.println();

    }

    sc.close();

    }

    }

Output:

* * * * * 

* * * * 

* * * 

* * 

*

7. Mirrored Right Triangle Star Pattern Programs in Java

import java.util.Scanner;

public class JigSawAcademy

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        System.out.println(“Enter number of rows: “); // to take input from user

        int rows = sc.nextInt();

        for (int i= 0; i<= rows; i++)

        {

            for (int j=1; j<=rows-i; j++)

            {

                System.out.print(” “);

            }

            for (int k=0;k<=i;k++)

            {

                System.out.print(“*”);

            } 

                System.out.println(“”);

        }

        sc.close();

    }

}

Output:

     *

    **

   ***

  ****

 *****

******

8. Reversed Pyramid Star Pattern Programs in Java

import java.util.Scanner;

public class JigSawAcademy

{

    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 (int j=0; j<=i; j++)

        {

            System.out.print(” “);

        }

        for (int k=0; k<=rows-1-i; k++)

        {

            System.out.print(“*” + ” “);

        }

        System.out.println();

    }

    sc.close();

}

}

Output:

 * * * * * 

  * * * * 

   * * * 

    * * 

     *

9. Right down Mirror Star Pattern Programs in Java

import java.util.Scanner;

public class JigSawAcademy

{

    public static void main(String[] args)

    {

    Scanner sc = new Scanner(System.in); // to take input

    System.out.println(“Enter number of rows: “);

    int rows = sc.nextInt();

    for (int i= rows; i>= 1; i–)

    {

    for (int j=rows; j>i;j–)

    {

    System.out.print(” “);

    }

    for (int k=1;k<=i;k++)

    {

    System.out.print(“*”);

    }

    System.out.println(“”);

    }

    sc.close();

    }

    }

Output:

*****

 ****

  ***

   **

    *

10. Diamond Number Pattern Programs in Java

import java.util.Scanner;

public class JigSawAcademy

{            

    public static void main(String[] args) {

        for (int i = 1; i <= 4; i++)

        {

            int n = 4;

            for (int j = 1; j<= n – i; j++) { System.out.print(” “); } for (int k = i; k >= 1; k–)

            {

                System.out.print(k);

            }

            for (int l = 2; l <= i; l++) { System.out.print(l); } System.out.println(); } for (int i = 3; i >= 1; i–)

        {

            int n = 3;

            for (int j = 0; j<= n – i; j++) { System.out.print(” “); } for (int k = i; k >= 1; k–)

            {

                System.out.print(k);

            }

            for (int l = 2; l <= i; l++)

            {

                System.out.print(l);

            }

            System.out.println();

        }

    }

}

Output:

   1

  212

 32123

4321234

 32123

  212

   1

11. Alphabet Pattern Programs in Java

import java.util.Scanner;

public class JigSawAcademy

{            

    {

        int alphabet = 65;

                for (int i = 0; i <= 5; i++)

        {

            for (int j = 0; j <= i; j++)

            {

                System.out.print((char) (alphabet + j) + ” “);

            }

            System.out.println();

        }

    }

}

Output:

A B 

A B C 

A B C D 

A B C D E 

A B C D E F

12. Character Pattern Programs in Java

Character pattern programs in java using for loop prints the patterns in shapes of characters.

import java.util.Scanner;

public class JigSawAcademy

{public static void main(String[] args)

{

for (int i = 5; i >= 0; i–)

{

   int alphabet = 65;

   for (int j = 0; j <= i; j++)

   {

       System.out.print((char) (alphabet + j) + ” “);

   }

   System.out.println();

}

for (int i = 0; i<= 5; i++)

{

   int alphabet = 65;

   for (int j = 0; j <= i; j++)

   {

       System.out.print((char) (alphabet + j) + ” “);

   }

   System.out.println();

}

}

}

Output:

A B C D E F 

A B C D E 

A B C D 

A B C 

A B 

A B 

A B C 

A B C D 

A B C D E 

A B C D E F

13. Square Pattern Programs in Java

import java.io.*; 

class  JigSawAcademy{ 

    //print hollow rectangle 

    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)             

                    System.out.print(“*”);             

                else

                    System.out.print(” “);             

            } 

            System.out.println(); 

        } 

    } 

    //Driver program

    public static void main(String args[]) 

    { 

        int rows = 6, columns = 20; 

        print_rectangle(rows, columns); 

    } 

}

Output:

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

* *

* *

* *

* *

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

14. Pattern Matching Pattern Programs in Java

public class REmatch {

    public static void main(String[] argv) {

        String patt = “Q[^u]\\d+\\.”;

        Pattern r = Pattern.compile(patt);

        String line = “Order QT300. Now!”;

        Matcher m = r.matcher(line);

        if (m.find()) {

            System.out.println(patt + ” matches \”” +

                m.group(0) +

                “\” in \”” + line + “\””);

        } else {

            System.out.println(“NO MATCH”);

        }

    }

}

Output:

Q[\^u]\d+\. matches “QT300.” in “Order QT300. Now!”

15. String Pattern Programs in Java

package com.javainterviewpoint;

import java.util.Scanner;

public class Pattern24

{

    public static void main(String[] args)

    {

        // to create a new Scanner object

        Scanner scanner = new Scanner(System.in);

        // to get the String from the user

        System.out.println(“Enter the String which needs to be printed “);

        String input = scanner.nextLine();

        System.out.println(“** Printing the pattern… **”);

        for (int i = 1; i <= input.length(); i++) 

        { 

            for (int j = input.length(); j > i; j–)

            {

                System.out.print(” “);

            }

            for (int k = i * 2 – 1; k >= 1; k–)

            {

                System.out.print(input.charAt(i – 1));

            }

            System.out.println(“”);

        }

    }

}

Output:

Enter the string which needs to be printed 

JAVA

** Printing the pattern… **

   J

  AAA

 VVVVV

AAAAAAA

16. Word Pattern Programs in Java

public boolean wordPattern(String pattern, String str) {

    String[] arr = str.split(” “);  

    //to prevent out of boundary problem

    if(arr.length != pattern.length())

        return false;

    HashMap<Character, String> map = new HashMap<Character, String>();

    for(int i=0; i<pattern.length(); i++){

        char c = pattern.charAt(i);

        if(map.containsKey(c)){

            String value = map.get(c);

            if(!value.equals(arr[i])){

                return false;

            }

        }else if (map.containsValue(arr[i])){

            return false;

        }

        map.put(c, arr[i]);

    }

    return true;

}

17. Odd Number Pattern Programs in Java

import java.util.Scanner; 

class Pattern { 

    static void display(int n) 

    { 

        int i, j, k; 

        for (i = 1; i <= n; i++) { 

            for (j = 1, k = i; j <= i; j++, k–) { 

                if (k % 2 == 0) { 

                    // to display the numbers 

                    System.out.print(j); 

                } 

                else { 

                    //to display the stars 

                    System.out.print(“*”); 

                } 

            } 

            System.out.print(“\n”); 

        } 

    } 

    // Driver Code 

    public static void main(String[] args) 

    { 

        // to get n 

        int n = 5; 

        // to print the pattern 

        display(n); 

    } 

}

Output:

*

1

*

2

*

1*3*

*

2*4

18. Pascal’s Triangle Pattern Programs in Java

import java.util.Scanner;

public class JigSawAcademy

{            

    public static void main(String[] args) {

        int n = 5;

        for (int i = 0; i < n; i++) {

            int number = 1;

            System.out.printf(“%” + (n – i) * 2 + “s”, “”);

            for (int j = 0; j <= i; j++) {

                System.out.printf(“%4d”, number);

                number = number * (i – j) / (j + 1);

            }

            System.out.println();

        }

    }

}

Output:

             1

           1 1

         1 2 1

       1 3 3 1

     1 4 6 4 1

19. Java Number Pattern Programs

Pattern number programs in java print numbers in patterns like square, Damon, sandglass, and triangle. 

import java.util.Scanner;

public class JigSawAcademy

{            

        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++ + ” “);

                }

                System.out.println();

            }

        }

    }

Output:

2 3 

4 5 6 

7 8 9 10 

11 12 13 14 15

20. Square Number Pattern Programs in Java 

import java.util.Scanner;

public class SquareNumber2 {

private static Scanner sc;

public static void main(String[] args) 

{

int side, i = 0, j;

sc = new Scanner(System.in);

System.out.print(” Please Enter any Side of a Square : “);

side = sc.nextInt();

while(i < side)

{

j = 0;

while(j < side)

{

System.out.print(“1”); 

j++;

}

i++;

System.out.print(“\n”); 

}

}

}

Output:

1 2 3

1 2 3

1 2 3

Similarly, there are and that you can print in the same way.

21. K Shape Alphabet Pattern Program

import java.util.Scanner;

public class JigSawAcademy

{public static void main(String[] args)

{

for (int i = 5; i >= 0; i–)

{

   int alphabet = 65;

   for (int j = 0; j <= i; j++)

   {

       System.out.print((char) (alphabet + j) + ” “);

   }

   System.out.println();

}

for (int i = 0; i<= 5; i++)

{

   int alphabet = 65;

   for (int j = 0; j <= i; j++)

   {

       System.out.print((char) (alphabet + j) + ” “);

   }

   System.out.println();

}

}

}

A B C D E F 

A B C D E 

A B C D 

A B C 

A B 

A B 

A B C 

A B C D 

A B C D E 

A B C D E F

22. Triangle Character Pattern Program in Java

public class JigSawAcademy

{            

    public static void main(String[] args)

{

        for (int i = 0; i <= 5; i++) { int alphabet = 65; for (int j = 5; j > i; j–)

        {

            System.out.print(” “);

        }

        for (int k = 0; k <= i; k++)

        {

            System.out.print((char) (alphabet + k) + ” “);

        }

        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

23. Diamond Pattern in Java

import java.util.Scanner;

public class JigSawAcademy

{

public static void main(String[] args) {

    char[] letter = { ‘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 letter_number = 0;

    String[] diamond = new String[26]; // create an array of strings

    System.out.print(“Enter any Character between A to Z : “);

    Scanner reader = new Scanner(System.in);

    try {

        char user_letter = reader.next(“[A-Z]”).charAt(0);

        // to search for letter number in the array letter

        for (int i = 0; i < letter.length; i++) {

            if (letter[i] == user_letter) {

                letter_number = i;

                break;

            }

        }

        // to construct a diamond

        for (int i = 0; i <= letter_number; i++) {

            diamond[i] = “”;

            // to add initial spaces

            for (int j = 0; j < letter_number – i; j++) {

                diamond[i] += ” “;

            }

            // to add letter

            diamond[i] += letter[i];

            // to add space between letters

            if (letter[i] != ‘A’) {

                for (int j = 0; j < 2 * i – 1; j++) { diamond[i] += ” “; } // to add letter diamond[i] += letter[i]; } 

// to draw the first section of the diamond 

System.out.println(diamond[i]); } for (int i = letter_number – 1; i >= 0; i–)

                {

            // to draw the second part of the diamond

            // to write the diamondArray in reverse order

            System.out.println(diamond[i]);

        }

    } catch (Exception e) {

        e.printStackTrace();

    } finally {

        reader.close();

    }

}

}

Output:

      A

    B B

   C C

  D D

 E E

F F

 E E

  D D

   C C

    B B

     A

24. Down triangle star program

import java.util.Scanner;

public class JigSawAcademy

{

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=rows; i>= 1 ; 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)) {

                System.out.print(“*”);

            }

            else {

                System.out.print(” “);

            }

        }

        System.out.println(“”);

    }

    sc.close();

}

}

Output:

*********

 * *

  * *

   * *

    *

25. Alphabet A Pattern

import java.util.Scanner;

public class JigSawAcademy

{

    // To print alphabet A pattern

    void display(int n)

    {

    // an outer for loop for number of lines

    for (int i = 0; i<=n; i++) {

    // an inner for loop for logic execution

    for (int j = 0; j<= n / 2; j++) {

    // to print two column lines

    if ((j == 0 || j == n / 2) && i != 0 ||

    // to print first line of alphabet

    i == 0 && j != n / 2 ||

    // to print middle line

    i == n / 2)

    System.out.print(“*”);

    else

    System.out.print(” “);

    }

    System.out.println();

    }

    }

    public static void main(String[] args)

    {

    Scanner sc = new Scanner(System.in);

JigSawAcademy a = new JigSawAcademy();

    a.display(7);

    }

    }

Output:

** 

* *

***

* *

* *

* *

26. Pattern Programs in Java

import java.util.Scanner;

public class JigSawAcademy

{            

    public static void main(String[] args) 

    {

        Scanner sc = new Scanner(System.in); // rows value from the user    

        System.out.println(“Enter the number of rows: “);    

        int rows = sc.nextInt();         

        for (int i = 1; i <= rows; i++) 

        {

            for (int j = 1; j <= i; j++)

            {

                System.out.print(i+” “);

            }

            System.out.println();

        }         

        sc.close();

    }

}

Output:

2 2 

3 3 3 

4 4 4 4 

5 5 5 5 5

27. Zeroes & ones Pattern Programs

import java.util.Scanner;

public class JigSawAcademy

{            

    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 = 1; j <= i; j++)

            {

                if(j%2 == 0)

                {

                    System.out.print(0);

                }

                else

                {

                    System.out.print(1);

                }

            }

            System.out.println();

        }

        sc.close();

    }

}

Output:

1

10

101

1010

10101

28. Binary Number Rectangle Pattern

import java.util.Scanner;

public class JigSawAcademy

{

    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++) 

        {

            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:

10101

01010

10101

01010

10101

29. Descending Numbers Pattern Program

import java.util.Scanner;

public class JigSawAcademy

{

    public static void main(String[] args) 

    {

        Scanner sc = new Scanner(System.in);

        //take a row-value from the user

        System.out.println(“How many rows do you want in this pattern?”);

        int rows = sc.nextInt();

        System.out.println(“Here is your pattern….!!!”);

        for (int i = 1; i <= rows; i++) 

        { 

            for (int j = rows; j >= i; j–)

            {

                System.out.print(j+” “);

            }

            System.out.println();

        }

        //to close all the resources

        sc.close();

    }

}

Output:

7 6 5 4 3 2 1

7 6 5 4 3 2 

7 6 5 4 3 

7 6 5 4

7 6 5

7 6

7

30. Matrix Pattern programs in java

import java.io.*; 

class JigSawAcademy

//to print the pattern 

static void printPattern(int n) 

    // the number of rows & columns to print

    int s = 2 * n – 1; 

    // upper half of matrix

    for (int i = 0; 

             i < (s / 2) + 1; i++)  

    { 

        int m = n; 

        // the decreasing part 

        for (int j = 0; j < i; j++)  

        { 

            System.out.print(m + ” “); 

            m–; 

        } 

        // the constant part 

        for (int k = 0;  

                 k < s – 2 * i; k++)  

        { 

            System.out.print(n – i + ” “); 

        } 

        // the increasing part. 

        m = n – i + 1; 

        for (int l = 0; l < i; l++)  

        { 

            System.out.print(m + ” “); 

            m++; 

        } 

        System.out.println(); 

    } 

    // lower half of the matrix

    for (int i = s / 2 – 1; 

             i >= 0; i–)  

    { 

        // the decreasing part 

        int m = n; 

        for (int j = 0; j < i; j++) 

        { 

            System.out.print(m + ” “); 

            m–; 

        } 

        // the constant part. 

        for (int k = 0;  

                 k < s – 2 * i; k++) 

        { 

            System.out.print(n – i + ” “); 

        } 

        // the decreasing part 

        m = n – i + 1; 

        for (int l = 0; l < i; l++)  

        { 

            System.out.print(m + ” “); 

            m++; 

        } 

        System.out.println(); 

    } 

// the driver code 

public static void main (String[] args)  

    int n = 3; 

    printPattern(n); 

}

Output:

3 3 3 3 3 

3 2 2 2 3 

3 2 1 2 3 

3 2 2 2 3 

3 3 3 3 3

Conclusion

This blog is a collection of different pattern programs in java; From basic pattern programs in java to difficult pattern programs in java. If you wish to learn more about Java programming, check out our Master Certificate in Full Stack Development program! The course offers Guaranteed Placement, live online instructor-led classes, and more!

Also Read

SHARE
share

Are you ready to build your own career?