Programs

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 

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.

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

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

    }

}

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

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?

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.

What 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).

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

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

Want to share this article?

Land on Your Dream Job

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN FULL STACK DEVELOPMENT
Apply Now

Leave a comment

Your email address will not be published. Required fields are marked *

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks