Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconFull Stack Developmentbreadcumb forward arrow iconTop 12 Pattern Programs in Java You Should Checkout Today

Top 12 Pattern Programs in Java You Should Checkout Today

Last updated:
21st Nov, 2022
Views
Read Time
12 Mins
share image icon
In this article
Chevron in toc
View All
Top 12 Pattern Programs in Java You Should Checkout Today

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.

Ads of upGrad blog

Explore Our Software Development Free Courses

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

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

Explore our Popular Software Engineering Courses

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

    }

}

In-Demand Software Development Skills

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

    }

}

Read our Popular Articles related to Software Development

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

Ads of upGrad blog

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.

Profile

Rohan Vats

Blog Author
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.

Frequently Asked Questions (FAQs)

1What is the scope of Java?

Java professionals have a bright future in India. Although it originates from C and C++, it is an object-oriented programming language with a simpler object model. It offers a virtual machine, known as the JVM, that is loaded with bytecode and can run on any system. As a result, pursuing a profession in the field of Java is both prudent and profitable. Java is expected to remain the first choice for most enterprises because of its robust community, enterprise support, and growing popularity among programmers. As a result, java job prospects are not moving away anytime soon. Java is a strongly typed language of programming used for web designing, Android development, enterprise-class applications, and the Internet of Things (IoT).

2What is the average salary of Java developers?

Web-based apps are created by Java developers. Java developers have to adjust the design and functioning of websites according to their clients' requests using the Java scripting language. Java Developers make an average of ₹4,82,846 per year. An entry-level Java Developer with less than a year of experience may expect to make a total salary of ₹2,94,582, which includes tips, bonuses, and overtime pay. The average total income for an early-career Java Developer with 1-4 years of experience is ₹4,45,758. The average total salary of ₹8,94,717 is earned by a Java Developer in their mid-career, with 5-9 years of experience. The average total compensation for an experienced Java Developer with 10-19 years of experience is ₹13,65,056.

3What is JVM?

The JVM, which is also known as Java Virtual Machine, is a run-time engine that allows Java applications to run. JVM is the program that compiles the main method in java programming. It is a component of JRE or the Java Runtime Environment. Java programs follow the principle of WORA (Write Once Run Anywhere). Through this method, a programmer can write Java code on one system, and this code can operate without modification on any other Java-enabled device. This is feasible because of JVM. Java code is first compiled into bytecode. On different machines, this bytecode is interpreted. Bytecode serves as a bridge between the host system and the Java source code. The Java Virtual Machine is responsible for memory allocation.

Explore Free Courses

Suggested Tutorials

View All

Suggested Blogs

Top 7 Node js Project Ideas &#038; Topics
31584
Node.JS is a part of the famous MEAN stack used for web development purposes. An open-sourced server environment, Node is written on JavaScript and he
Read More

by Rohan Vats

05 Mar 2024

How to Rename Column Name in SQL
46947
Introduction We are surrounded by Data. We used to store information on paper in enormous file organizers. But eventually, we have come to store it o
Read More

by Rohan Vats

04 Mar 2024

Android Developer Salary in India in 2024 [For Freshers &#038; Experienced]
901334
Wondering what is the range of Android Developer Salary in India? Software engineering is one of the most sought after courses in India. It is a reno
Read More

by Rohan Vats

04 Mar 2024

7 Top Django Projects on Github [For Beginners &amp; Experienced]
52129
One of the best ways to learn a skill is to use it, and what better way to do this than to work on projects? So in this article, we’re sharing t
Read More

by Rohan Vats

04 Mar 2024

Salesforce Developer Salary in India in 2024 [For Freshers &#038; Experienced]
909210
Wondering what is the range of salesforce salary in India? Businesses thrive because of customers. It does not matter whether the operations are B2B
Read More

by Rohan Vats

04 Mar 2024

15 Must-Know Spring MVC Interview Questions
34763
Spring has become one of the most used Java frameworks for the development of web-applications. All the new Java applications are by default using Spr
Read More

by Arjun Mathur

04 Mar 2024

Front End Developer Salary in India in 2023 [For Freshers &#038; Experienced]
902394
Wondering what is the range of front end developer salary in India? Do you know what front end developers do and the salary they earn? Do you know wh
Read More

by Rohan Vats

04 Mar 2024

Method Overloading in Java [With Examples]
26265
Java is a versatile language that follows the concepts of Object-Oriented Programming. Many features of object-oriented programming make the code modu
Read More

by Rohan Vats

27 Feb 2024

50 Most Asked Javascript Interview Questions &#038; Answers [2024]
4394
Javascript Interview Question and Answers In this article, we have compiled the most frequently asked JavaScript Interview Questions. These questions
Read More

by Kechit Goyal

26 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon