Introduction

If you aspire to become a Java professional, then Pattern Programs in Java is one topic that you should prepare well. The pattern-based Java programs help analyze programming ability, and we have tried to provide pattern programs in Java with detailed explanations. This blog will be a comprehensive guide for how to do all pattern programs in Java. 

A) 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++) // number of rows(n) 

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

            { 

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

            }  

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

            { 

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

            } 

            System.out.println(); //ending line after each 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. 

B) Star Pattern Programs in Java

Star pattern programs in java using a while loop creates many shaped patterns like pascal triangle, down triangle, and right-angle triangle pattern programs in Java. Some of the pattern programs in java using for loop are given here.

1. Right Triangle Star Pattern Programs in Java

public class RightTrianglePattern  

{   

public static void main(String args[])   

{   

// rows  to print  

int i, j, row=6;   

//outer loop for rows  

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

{   

//inner loop for columns  

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

{   

//prints stars   

System.out.print(“* “);   

}   

// new line 

System.out.println();   

}   

}   

Output:

*

* *

* * *

* * * *

* * * * *

2. 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++) //outer loop for number of rows(n) 

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

            {           

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

            } 

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

            {       

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

            }           

            System.out.println(); // ending line after each row

        } 

    } 

    public static void main(String args[]) 

    { 

        int n = 5; 

        printStars(n); 

    } 

}

Output:

           * 

         * * 

       * * * 

     * * * * 

   * * * * *

3. 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:

    *

   ***

  *****

 *******

*********

 *******

  *****

   ***

    *

4. 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: “); //takes 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:

* * * * * 

* * * * 

* * * 

* * 

*

5. 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: “); // takes 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:

     *

    **

   ***

  ****

 *****

******

6. 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:

 * * * * * 

  * * * * 

   * * * 

    * * 

     *

7. 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); // takes 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:

*****

 ****

  ***

   **

    *

8. Number Pattern Programs in Java

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

9. 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.

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

{            

    public static void main(String[] args)

    {

        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 GFG { 

    // Function to 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 for above function 

    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)

    {

        // Create a new Scanner object

        Scanner scanner = new Scanner(System.in);

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

    //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. Matrix Pattern programs in java

import java.io.*; 

class GFG  

// Function to  

// print the pattern 

static void printPattern(int n) 

    // number of rows and  

    // columns to be printed 

    int s = 2 * n – 1; 

    // Upper Half 

    for (int i = 0; 

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

    { 

        int m = n; 

        // Decreasing part 

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

        { 

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

            m–; 

        } 

        // Constant Part 

        for (int k = 0;  

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

        { 

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

        } 

        // Increasing part. 

        m = n – i + 1; 

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

        { 

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

            m++; 

        } 

        System.out.println(); 

    } 

    // Lower Half 

    for (int i = s / 2 – 1; 

             i >= 0; i–)  

    { 

        // Decreasing Part 

        int m = n; 

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

        { 

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

            m–; 

        } 

        // Constant Part. 

        for (int k = 0;  

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

        { 

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

        } 

        // Decreasing Part 

        m = n – i + 1; 

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

        { 

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

            m++; 

        } 

        System.out.println(); 

    } 

// 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 

18. 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) { 

                    // displaying the numbers 

                    System.out.print(j); 

                } 

                else { 

                    // displaying the stars 

                    System.out.print(“*”); 

                } 

            } 

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

        } 

    } 

    // Driver Code 

    public static void main(String[] args) 

    { 

        // Get n 

        int n = 5; 

        // Print the pattern 

        display(n); 

    } 

}

Output:

*

1

*

2

*

1*3*

*

2*4

19. 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

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!

Also, Read

SHARE
share

Are you ready to build your own career?