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
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.
This article originally published on upGrad blog.