Top 15+ Basic and Advanced Pattern Programs in Java | Java Pattern Program

Top 15+ Basic and Advance Pattern Programs in Java

Hello Friends, How are you? Today I am sharing the top 15+ basic and advanced pattern programs in java. I have classified the Pattern Programs in Java into mainly Star Pattern, Number Pattern and Character Pattern.

{tocify} $title={Table of Contents}

This Java Pattern Program will help you to enhance your coding skill, logic and looping concepts. This type of pattern problem is mostly asked in Java Interview to check the logical thinking of the programmer. In this post, you will get several designs of pattern programs that will help you to understand the logic of a program.

Basic and Advanced Pattern Programs in Java


Note: You must have a deep knowledge of the Java loop, such as for loop, do-while loop to solve the pattern programs in java.

First, we will see 9 Basic Pattern Programs in Java, after that we will see 6 Advance Pattern Programs.

Top 9 Basic Pattern Programs in Java - Pattern Program

1.  Star Rectangle Pattern Program in Java

★ ★ ★ ★ ★ ★ ★ 
★ ★ ★ ★ ★ ★ ★ 
★ ★ ★ ★ ★ ★ ★ 
★ ★ ★ ★ ★ ★ ★ 

public class pattern {
    public static void main(String[] args) {

        int m=4, n=8; //m=rows, n=column
        
        //1. Rectangle Pattern

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

2. Hollow Rectangle Star Pattern Program in Java

★ ★ ★ ★ ★ ★ ★ 
★                           
★                           
★ ★ ★ ★ ★ ★ ★ 

public class pattern {
    public static void main(String[] args) {

        int m=4, n=8; //m=rows, n=column

        //2. Hollow Rectangle Start Pattern
        for(int i=1; i<=m; i++) {
            for (int j=1; j<=n; j++) {
                if(i==1 || j==1 || i==m || j==n) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }
            System.out.println();
        }
    }
}

3. Half Pyramid Star Pattern Program in Java

★ 
★ ★ 
★ ★ ★ 
★ ★ ★ ★ 

public class pattern {
    public static void main(String[] args) {

        int m=5; //m=rows

        //3. Half Pyramid
        for(int i=1; i<=m; i++) {
            for(int j=1; j<=i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

4. Half Inverted Pyramid Star Pattern Program in Java

★ ★ ★ ★ 
★ ★ ★ 
★ ★ 
★ 

public class pattern {
    public static void main(String[] args) {

        int m=5; //m=rows

        // 4. Inverted Half Pyramid
        for(int i=m; i>=1; i--) {
            for (int j=1; j<=i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

5. Half Inverted Pyramid (Rotated by 180 deg) Star Pattern

                 
             ★ 
         ★ ★ 
    ★ ★ ★ 
★ ★ ★ ★ 

public class pattern {
    public static void main(String[] args) {

        int m=5; //m=rows
       
        //5. Half Inverted Pyramid (Rotated by 180 deg)

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

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

6. Half Pyramid Number Pattern

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

public class pattern {
    public static void main(String[] args) {

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

7. Inverted Half Pyramid Number Pattern

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

public class pattern {
    public static void main(String[] args) {

        int m=5; //m=rows
       
        //7. Inverted Half Pyramid with Numbers
        for(int i=m; i>=1; i--) {
            for (int j=1; j<=i; j++) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

8. Floyd's Triangle Number Pattern

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

public class pattern {
    public static void main(String[] args) {

        int m=5; //m=rows
       
        //8. Floyd's Traingle
        int number = 1;
        for(int i=1; i<=m; i++) {
            for(int j=1; j<=i; j++) {
                System.out.print(number + " ");
                number++;
            }
            System.out.println();
        }
    }
}

9. 0 - 1 Triangle Number Pattern

1
0 1
1 0 1
0 1 0 1
1 0 1 0 1

public class pattern {
    public static void main(String[] args) {

        int m=5; //m=rows
       
        for(int i=1; i<=m; i++) {
            for(int j=1; j<=i; j++) {
                if((i+j)%2==0) {
                    System.out.print("1 ");
                } else {
                    System.out.print("0 ");
                }
            }
            System.out.println();
        }
    }
}

Top 6 Advance Pattern Programs in Java - Pattern Program

1. Butterfly Star Pattern in Java

                                     
★                             ★ 
★ ★                    ★ ★ 
★ ★ ★           ★ ★ ★ 
★ ★ ★ ★ ★ ★ ★ ★ ★ 
★ ★ ★ ★ ★ ★ ★ ★ ★ 
★ ★ ★ ★          ★ ★ ★ 
★ ★ ★                   ★ ★ 
★ ★                            ★ 
★                                     

public class pattern {
    public static void main(String[] args) {

        int n=5; //n=rows
       
       //1. Butterfly Pattern
        //upper half
        for(int i=1; i<=n; i++) {
            //first part
            for(int j=1; j<=i; j++) {
                System.out.print("* ");
            }

            //spaces
            int space = 2*(n-i);
            for(int j=1; j<=space; j++) {
                System.out.print("  ");
            }

            //second part
            for(int j=1; j<=i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
        //lower half
        for(int i=n; i>=1; i--) {
            //first part
            for(int j=1; j<=i; j++) {
                System.out.print("* ");
            }

            //spaces
            int space = 2*(n-i);
            for(int j=1; j<=space; j++) {
                System.out.print("  ");
            }

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

2. Solid Rhombus Star Pattern in Java

                    ★ ★ ★ ★ 
               ★ ★ ★ ★ 
          ★ ★ ★ ★ 
     ★ ★ ★ ★ 
★ ★ ★ ★ 

public class pattern {
    public static void main(String[] args) {

        int n=5; //n=rows
       
      //2. Solid Rhombus
        for(int i=1; i<=n; i++) {
            //spaces
            for(int j=1; j<=n-i; j++) {
                System.out.print("  ");
            }

            //stars
            for(int j=1; j<=n; j++) {
                System.out.print("* ");
            }

            System.out.println();
        }
    }
}

3. Hollow Rhombus Star Pattern in Java

★ ★ ★ 
★ ★                     
★                     ★ 
                    ★ ★ 
                   ★ ★ ★ 

public class pattern {
    public static void main(String[] args) {

        int n=5; //n=rows
       
      //3. Hollow Rhombus
        for(int i=1; i<=n; i++) {
            //spaces
            for(int j=1; j<=n-i; j++) {
                System.out.print("* ");
            }

            //stars
            for(int j=1; j<=n; j++) {
                System.out.print("  ");
            }
            
            for(int j=1; j<=i-1; j++) {
                System.out.print("* ");
            }

            System.out.println();
        }
    }
}

4. Complete Number Pyramid Pattern in Java

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

public class pattern {
    public static void main(String[] args) {

        int n=5; //n=rows
       
      //4. Number Pyramid
        for(int i=1; i<=n; i++) {
            //spaces
            for(int j=1; j<=n-i; j++) {
                System.out.print(" ");
            }
            //numbers
            for(int j=1; j<=i; j++) {
                System.out.print(i + " ");
            }
            System.out.println();
        }
    }
}

5. Palindromic Number Pattern in Java

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

public class pattern {
    public static void main(String[] args) {

        int n=5; //n=rows
       
      //5. Palindromic Pattern
        for(int i=1; i<=n; i++) {
            //spaces
            for(int j=1; j<=n-i; j++) {
                System.out.print("  ");
            }
            //first half numbers
            for(int j=i; j>=1; j--) {
                System.out.print(j + " ");
            }
            //second half numbers
            for(int j=2; j<=i; j++) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}

6. Palindromic Number Pattern in Java

                 
             ★ ★ 
         ★ ★ ★ ★ 
     ★ ★ ★ ★ ★ ★ 
★ ★ ★ ★ ★ ★ ★ ★ 
     ★ ★ ★ ★ ★ ★ 
         ★ ★ ★ ★ 
             ★ ★ 
                 

public class pattern {
    public static void main(String[] args) {

        int n=5; //n=rows
       
     //6. Daimond Pattern
        for(int i=1; i<=n; i++) {
            //spaces
            for(int j=1; j<=n-i; j++) {
                System.out.print("  ");
            }
            //stars
            for(int j=1; j<=2*i-1; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
        for(int i=n-1; i>=1; i--) {
            //spaces
            for(int j=1; j<=n-i; j++) {
                System.out.print("  ");
            }
            //stars
            for(int j=1; j<=2*i-1; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Important Notes - Pattern Program

If you want to run these pattern programs directly then first of all create a java file name pattern.java. Make sure the name file name should be exactly pattern.java. Otherwise, you'll get an error. So please take care of it. {alertInfo}

I hope you have understood these basic and advanced pattern programs in java. Try to solve these star and number pattern programs in java on your own. It will help you to improve further in coding. If you have any queries regarding this post please comment below or contact us.

All the Best!

Post a Comment

Previous Post Next Post