View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Top 12 Pattern Programs in Java You Should Checkout Today

By Rohan Vats

Updated on Nov 22, 2022 | 12 min read | 21.4k views

Share:

Preparing for technical interviews is tricky, and if you’re a Java professional, things are more complicated. A popular method of analyzing a Java professional’s expertise is by seeing how well one can make a pattern program in Java. You might have to make a unique Java pattern program, which is not prevalent to nail the interview.

Don’t worry because, in this article, we’ll take a look at multiple Java patterns so you can get a better understanding of the same. The article will also highlight some of the popular design patterns in Java, such as singleton design patterns, and factory design patterns, among others. 

All of the patterns we’ve discussed here are made of digits. The best way to practice these patterns would be to try to create the first and if you struggle at some point, compare your code with ours. This way, you’ll understand what does what and would not have any confusion regarding these programs.

Check out our free courses to get an edge over the competition.

We recommend working your way up from the first pattern. If you have some experience creating a Java pattern program, you can start with any of the designs we’ve shared below:

Read: Java Developer Salary in India

Check out upGrad: Java Bootcamp

What is pattern printing in Java?

Before we delve into the various design patterns in Java, let’s take a look at what exactly pattern printing in Java refers to. To put it simply, pattern printing programs are patterns or symbols that contain alphabets, or characters in a particular form. These programs mainly aim to improve logic, coding skills, and looping concepts. A Java pattern program specifically can be printed in various designs.

Simple Triangle Pattern

Pattern: 

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1 2 3 4 5 6

1 2 3 4 5 6 7

Code:

import java.util.Scanner;

public class MainClass

{

    public static void main(String[] args) 

    {

        Scanner sc = new Scanner(System.in);        

        //Taking rows value from the user   

        System.out.println(“How many rows 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 = 1; j <= i; j++)

            {

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

            }             

            System.out.println();

        }         

        //Close the resources         

        sc.close();

    }

}

Check out upGrad: Advanced Certification in Blockchain

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months
View Program

Job-Linked Program

Bootcamp36 Weeks
View Program

Double Triangle Pattern

Pattern:

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1 2 3 4 5 6 7

1 2 3 4 5 6

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

Code:

import java.util.Scanner; 

public class MainClass

{

    public static void main(String[] args) 

    {

        Scanner sc = new Scanner(System.in);         

        //Taking rows value from the user        

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

        int rows = sc.nextInt();         

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

        //Printing upper half of the pattern         

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

        {

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

            { 

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

            }              

            System.out.println(); 

        }          

        //Printing lower half of the pattern          

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

        {

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

            {

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

            }            

            System.out.println();

        }        

        //Closing the resources         

        sc.close();

    }

}

Learn more: Top 8 Java Projects on Github You Should Get Your Hands-on
 

upGrad’s Exclusive Software and Tech Webinar for you –

SAAS Business – What is So Different?

Triangle Pattern with Repeated Digits

Pattern:

1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

6 6 6 6 6 6

7 7 7 7 7 7 7 

Code:

import java.util.Scanner;

public class MainClass

{

    public static void main(String[] args) 

    {

        Scanner sc = new Scanner(System.in);       

        //Taking rows value from the user         

        System.out.println(“How many rows 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 = 1; j <= i; j++)

            {

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

            }             

            System.out.println();

        }         

        //Close the resources         

        sc.close();

    }

}

Inverted Triangle with Descending Digits

Pattern:

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

Code:

import java.util.Scanner;

public class MainClass

{

    public static void main(String[] args) 

    {

        Scanner sc = new Scanner(System.in);         

        //Taking rows value from the user         

        System.out.println(“How many rows 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();

        }         

        //Closing the resources         

        sc.close();

    }

}

Triangle with Repeating Pattern

Pattern:

1

1 2 1

1 2 3 2 1

1 2 3 4 3 2 1

1 2 3 4 5 4 3 2 1

1 2 3 4 5 6 5 4 3 2 1

1 2 3 4 5 6 7 6 5 4 3 2 1

Code:

import java.util.Scanner;

public class MainClass

{

    public static void main(String[] args) 

    {

        Scanner sc = new Scanner(System.in);         

        //Taking rows value from the user         

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

        int rows = sc.nextInt();         

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

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

        {

            //Printing first half of the row            

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

            { 

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

            }             

            //Printing second half of the row              

            for (int j = i-1; j >= 1; j–)

            {

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

            }             

            System.out.println();

        }         

        //Closing the resources        

        sc.close();

    }

}

Double Triangle Pattern

Pattern:

1 2 3 4 5 6 7

1 2 3 4 5 6

1 2 3 4 5

1 2 3 4

1 2 3

1 2 

1 2

1 2 3 

1 2 3 4 

1 2 3 4 5

1 2 3 4 5 6

1 2 3 4 5 6 7

Code:

import java.util.Scanner; 

public class MainClass

{

    public static void main(String[] args) 

    {

        Scanner sc = new Scanner(System.in);         

        //Taking rows value from the user         

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

        int rows = sc.nextInt();        

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

        //Printing upper half of the pattern         

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

        {

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

            {

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

            }             

            System.out.println();

        }         

        //Printing lower half of the pattern         

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

        {

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

            {

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

            }             

            System.out.println();

        }         

        //Closing the resources        

        sc.close();

    }

}

Inverted Double Triangles

Pattern:

1234567

  234567

    34567

      4567

        567

          67

            7

          67

        567

      4567

    34567

  234567

1234567

Code:

import java.util.Scanner;

public class MainClass

{

    public static void main(String[] args) 

    {

        Scanner sc = new Scanner(System.in);        

        //Taking rows value from the user        

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

        int rows = sc.nextInt();      

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

        //Printing upper half of the pattern         

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

        {

            //Printing i spaces at the beginning of each row             

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

            {

                System.out.print(” “);

            }             

            //Printing i to rows value at the end of each row             

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

            { 

                System.out.print(j); 

            }              

            System.out.println(); 

        }          

        //Printing lower half of the pattern          

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

        {

            //Printing i spaces at the beginning of each row             

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

            {

                System.out.print(” “);

            }            

            //Printing i to rows value at the end of each row            

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

            {

                System.out.print(j);

            }             

            System.out.println();

        }        

        //Closing the resources         

        sc.close();

    }

}

Double Inverted Triangles

Pattern:

1 2 3 4 5 6 7 

  2 3 4 5 6 7

    3 4 5 6 7

      4 5 6 7

        5 6 7

          6 7

            7

          6 7

        5 6 7

      4 5 6 7

    3 4 5 6 7

  2 3 4 5 6 7

1 2 3 4 5 6 7

Code:

import java.util.Scanner; 

public class MainClass

{

    public static void main(String[] args) 

    {

        Scanner sc = new Scanner(System.in);        

        //Taking rows value from the user     

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

        int rows = sc.nextInt();         

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

        //Printing upper half of the pattern         

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

        {

            //Printing i spaces at the beginning of each row             

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

            {

                System.out.print(” “);

            }             

            //Printing i to rows value at the end of each row             

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

            { 

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

            }            

            System.out.println(); 

        }          

        //Printing lower half of the pattern          

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

        {

            //Printing i spaces at the beginning of each row             

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

            {

                System.out.print(” “);

            }             

            //Printing i to rows value at the end of each row             

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

            {

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

            }            

            System.out.println();

        }         

        //Closing the resources        

        sc.close();

    }

}

Digit Pillar Pattern

Pattern:

1111111

1111122

1111333

1114444

1155555

1666666

7777777 

Code:

import java.util.Scanner; 

public class MainClass

{

    public static void main(String[] args) 

    {

        Scanner sc = new Scanner(System.in);         

        System.out.println(“How many rows 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 = 1; j <= rows-i; j++)

            {

                System.out.print(1);

            }             

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

            {

                System.out.print(i);

            }             

            System.out.println();

        }         

        sc.close();

    }

}

Binary Digit Pattern

Pattern:

1010101

0101010

1010101

0101010

1010101

0101010

1010101

Code:

import java.util.Scanner;

public class MainClass

{

    public static void main(String[] args) 

    {

        Scanner sc = new Scanner(System.in);        

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

        int rows = sc.nextInt();        

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

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

    }

}

Ascending Triangle Pattern

Pattern:

1

2 6

3 7 10 

4 8 11 13

5 9 12 14 15

Code:

import java.util.Scanner;

public class MainClass

{

    public static void main(String[] args) 

    {

        Scanner sc = new Scanner(System.in);      

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

        int rows = sc.nextInt();         

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

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

        {

            int num = i;             

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

            {

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

                num = num+rows-j;

            }             

            System.out.println();

        }         

        sc.close();

    }

}

Square Java Pattern Program

Pattern:

1 2 3 4 5 6 7 

2 3 4 5 6 7 1 

3 4 5 6 7 1 2 

4 5 6 7 1 2 3 

5 6 7 1 2 3 4 

6 7 1 2 3 4 5 

7 1 2 3 4 5 6 

Code:

import java.util.Scanner; 

public class MainClass

{

    public static void main(String[] args) 

    {

        Scanner sc = new Scanner(System.in);         

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

        int rows = sc.nextInt();         

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

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

        {

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

            {

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

            }

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

            {

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

            }

            System.out.println();

        }       

        sc.close();

    }

}

Also Read: Python vs Java in 2020: Which One You Should Choose?

Star Pattern In Java

Star pattern is considered to be one of the most common design pattern programs in Java, which is mainly used to improve logical thinking, as well as flow control knowledge. In order to display star patterns in Java, two to three loops are generally required, depending on the programs. The first loop of the star pattern in Java is also sometimes referred to as the outer loop, whereas the second loop is the inner one, which contains rows and columns. 

Factory Design Pattern In Java

Factory Design pattern in Java is yet another fundamental method used in Java. It is mainly used when there are multiple sub-classes with a superclass, and you need to return one of the sub-classes based on the input. This method successfully eliminates the responsibility of the instantiation of a classroom the client program to the factory class. There are a ton of advantages that you can derive from implementing factory design patterns in Java. Some of them might include, with factory design patterns, codes getting more robust and easy to extend. Furthermore, factory design pattern also allows abstraction between implementation and client classes. 

Singleton Design Pattern Java

There are mainly two types of singleton design pattern Java has, namely, early instantiation and lazy instantiation. In this design pattern, a class ensures that only one instance has been created, which can then be used by all the other classes. One of the top advantages of using the Singleton Design Pattern in Java is that you save a lot of memory since only one instance is used every time by all the other classes.

Final Thoughts

We are sure that you’re ready to create a pattern program in Java after going through this list. You can start with any pattern according to your experience and expertise. If you have any questions regarding this topic or this article, please let us know in the comment section below.

If you’re interested to learn more about Java, full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Frequently Asked Questions (FAQs)

1. What is the scope of Java?

2. What is the average salary of Java developers?

3. What is JVM?

Rohan Vats

408 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive PG Certification in AI-Powered Full Stack Development

77%

seats filled

View Program

Top Resources

Recommended Programs

upGrad

AWS | upGrad KnowledgeHut

AWS Certified Solutions Architect - Associate Training (SAA-C03)

69 Cloud Lab Simulations

Certification

32-Hr Training by Dustin Brimberry

View Program
upGrad

Microsoft | upGrad KnowledgeHut

Microsoft Azure Data Engineering Certification

Access Digital Learning Library

Certification

45 Hrs Live Expert-Led Training

View Program
upGrad

upGrad KnowledgeHut

Professional Certificate Program in UI/UX Design & Design Thinking

#1 Course for UI/UX Designers

Bootcamp

3 Months

View Program