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.
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Â
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
Why Learn to Code? How Learn to Code? | How to Install Specific Version of NPM Package? | Types of Inheritance in C++ What Should You Know? |
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.
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.