For aspiring Java professionals, Java Pattern Programs are critical to understand and practice as the pattern-based Java programs help analyze programming ability. In this listicle, we have tried to provide pattern programs in Java with detailed explanations. This blog will be a comprehensive guide for how to print patterns in Java.
One of the most essential components of the Java Interview questions has always been Java Pattern Programs. Although they appear to be hard to solve at times, most often than not, they are just really based on mathematical logic and the principles of matrices.Therefore, pattern programs in java are always in high demand. Patterns can be designed in a variety of creative, architectural, and behavioral ways to provide answers for instantiating an object in the best feasible way for specific conditions, depending on the pattern structure type. When designing or changing applications or software programs, software designers employ Design Patterns as a prominent problem-solving strategy. It aids in the creation of simple, well-structured software that testers, developers, and users can understand and utilize. Maintaining and introducing additional attributions in accordance with version requirements is simple; errors may be detected and promptly fixed if they occur. If necessary, developers can reuse programs; work on multiple interfaces and bridge them.
The pyramid is one of the simple pattern programs in Java that is often asked in interviews.
public class JigSawAcademy
{
public static void PyramidPattern(int n)
for (int i=0; i<n; i++) // for number of rows(n)
{ for (int j=n-i; j>1; j–) // a loop for spaces
System.out.print(” “); //to print space
}
for (int j=0; j<=i; j++ ) //for number of columns
System.out.print(“* “); //to print star
System.out.println(); //end-line after every row
public static void main(String args[]) //driver function,
int n = 5;
PyramidPattern(n);
Output:
*
* *
* * *
* * * *
* * * * *
It is one of the easiest pattern printing programs in Java.
import java.util.Scanner;
public static void main(String[] args)
Scanner sc = new Scanner(System.in);
System.out.println(“Enter the number of rows: “);
int rows = sc.nextInt();
for (int i= 0; i<= rows-1 ; i++)
for (int j=0; j <i; j++)
System.out.print(” “);
for (int k=i; k<=rows-1; k++) { System.out.print(“*” + ” “); } System.out.println(“”); } for (int i= rows-1; i>= 0; i–)
for (int j=0; j< i ;j++)
for (int k=i; k<=rows-1; k++)
System.out.print(“*” + ” “);
System.out.println(“”);
sc.close();
public class RightTrianglePattern
public static void main(String args[])
// the rows to print
int i, j, row=6;
//the outer loop for rows
for(i=0; i<row; i++)
//the inner loop for columns
for(j=0; j<=i; j++)
//to prints stars
System.out.print(“* “);
// print new line
System.out.println();
public static void printStars(int n)
int i, j;
for(i=0; i<n; i++) //an outer loop for number of rows(n)
{ for(j=2*(n-i); j>=0; j–) // an inner loop for spaces
System.out.print(” “); // to print space
for(j=0; j<=i; j++) // an inner loop for columns
System.out.print(“* “); // to print star
System.out.println(); // end-line after every row
printStars(n);
int n, i, j, space = 1;
System.out.print(“Enter the number of rows: “);
Scanner s = new Scanner(System.in);
n = s.nextInt();
space = n – 1;
for (j = 1; j<= n; j++)
for (i = 1; i<= space; i++)
space–;
for (i = 1; i <= 2 * j – 1; i++)
System.out.print(“*”);
space = 1;
for (j = 1; j<= n – 1; j++)
space++;
for (i = 1; i<= 2 * (n – j) – 1; i++)
***
*****
*******
*********
System.out.println(“Enter the number of rows: “); //to take input from user
for (int i= rows-1; i>=0 ; i–)
for (int j=0; j<=i; j++)
System.out.println(“Enter number of rows: “); // to take input from user
for (int i= 0; i<= rows; i++)
for (int j=1; j<=rows-i; j++)
for (int k=0;k<=i;k++)
**
****
******
for (int k=0; k<=rows-1-i; k++)
Scanner sc = new Scanner(System.in); // to take input
System.out.println(“Enter number of rows: “);
for (int i= rows; i>= 1; i–)
for (int j=rows; j>i;j–)
for (int k=1;k<=i;k++)
public static void main(String[] args) {
for (int i = 1; i <= 4; i++)
int n = 4;
for (int j = 1; j<= n – i; j++) { System.out.print(” “); } for (int k = i; k >= 1; k–)
System.out.print(k);
for (int l = 2; l <= i; l++) { System.out.print(l); } System.out.println(); } for (int i = 3; i >= 1; i–)
int n = 3;
for (int j = 0; j<= n – i; j++) { System.out.print(” “); } for (int k = i; k >= 1; k–)
for (int l = 2; l <= i; l++)
System.out.print(l);
1
212
32123
4321234
int alphabet = 65;
for (int i = 0; i <= 5; i++)
for (int j = 0; j <= i; j++)
System.out.print((char) (alphabet + j) + ” “);
A
A B
A B C
A B C D
A B C D E
A B C D E F
Character pattern programs in java using for loop prints the patterns in shapes of characters.
{public static void main(String[] args)
for (int i = 5; i >= 0; i–)
for (int i = 0; i<= 5; i++)
import java.io.*;
class JigSawAcademy{
//print hollow rectangle
static void print_rectangle(int n, int m)
for (i = 1; i <= n; i++)
for (j = 1; j <= m; j++)
if (i == 1 || i == n ||
j == 1 || j == m)
else
//Driver program
int rows = 6, columns = 20;
print_rectangle(rows, columns);
********************
public class REmatch {
public static void main(String[] argv) {
String patt = “Q[^u]\\d+\\.”;
Pattern r = Pattern.compile(patt);
String line = “Order QT300. Now!”;
Matcher m = r.matcher(line);
if (m.find()) {
System.out.println(patt + ” matches \”” +
m.group(0) +
“\” in \”” + line + “\””);
} else {
System.out.println(“NO MATCH”);
Q[\^u]\d+\. matches “QT300.” in “Order QT300. Now!”
package com.javainterviewpoint;
public class Pattern24
// to create a new Scanner object
Scanner scanner = new Scanner(System.in);
// to get the String from the user
System.out.println(“Enter the String which needs to be printed “);
String input = scanner.nextLine();
System.out.println(“** Printing the pattern… **”);
for (int i = 1; i <= input.length(); i++)
for (int j = input.length(); j > i; j–)
for (int k = i * 2 – 1; k >= 1; k–)
System.out.print(input.charAt(i – 1));
Enter the string which needs to be printed
JAVA
** Printing the pattern… **
J
AAA
VVVVV
AAAAAAA
public boolean wordPattern(String pattern, String str) {
String[] arr = str.split(” “);
//to prevent out of boundary problem
if(arr.length != pattern.length())
return false;
HashMap<Character, String> map = new HashMap<Character, String>();
for(int i=0; i<pattern.length(); i++){
char c = pattern.charAt(i);
if(map.containsKey(c)){
String value = map.get(c);
if(!value.equals(arr[i])){
}else if (map.containsValue(arr[i])){
map.put(c, arr[i]);
return true;
class Pattern {
static void display(int n)
int i, j, k;
for (i = 1; i <= n; i++) {
for (j = 1, k = i; j <= i; j++, k–) {
if (k % 2 == 0) {
// to display the numbers
System.out.print(j);
else {
//to display the stars
System.out.print(“\n”);
// Driver Code
// to get n
// to print the pattern
display(n);
2
1*3*
2*4
for (int i = 0; i < n; i++) {
int number = 1;
System.out.printf(“%” + (n – i) * 2 + “s”, “”);
for (int j = 0; j <= i; j++) {
System.out.printf(“%4d”, number);
number = number * (i – j) / (j + 1);
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Pattern number programs in java print numbers in patterns like square, Damon, sandglass, and triangle.
int i, j, k = 1;
for (i = 1; i <= 5; i++) {
for (j = 1; j< i + 1; j++) {
System.out.print(k++ + ” “);
2 3
4 5 6
7 8 9 10
11 12 13 14 15
public class SquareNumber2 {
private static Scanner sc;
int side, i = 0, j;
sc = new Scanner(System.in);
System.out.print(” Please Enter any Side of a Square : “);
side = sc.nextInt();
while(i < side)
j = 0;
while(j < side)
System.out.print(“1”);
j++;
i++;
1 2 3
Similarly, there are and that you can print in the same way.
for (int i = 0; i <= 5; i++) { int alphabet = 65; for (int j = 5; j > i; j–)
for (int k = 0; k <= i; k++)
System.out.print((char) (alphabet + k) + ” “);
char[] letter = { ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’,
‘K’, ‘L’, ‘M’, ‘N’, ‘O’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’,
‘W’, ‘X’, ‘Y’, ‘Z’ };
int letter_number = 0;
String[] diamond = new String[26]; // create an array of strings
System.out.print(“Enter any Character between A to Z : “);
Scanner reader = new Scanner(System.in);
try {
char user_letter = reader.next(“[A-Z]”).charAt(0);
// to search for letter number in the array letter
for (int i = 0; i < letter.length; i++) {
if (letter[i] == user_letter) {
letter_number = i;
break;
// to construct a diamond
for (int i = 0; i <= letter_number; i++) {
diamond[i] = “”;
// to add initial spaces
for (int j = 0; j < letter_number – i; j++) {
diamond[i] += ” “;
// to add letter
diamond[i] += letter[i];
// to add space between letters
if (letter[i] != ‘A’) {
for (int j = 0; j < 2 * i – 1; j++) { diamond[i] += ” “; } // to add letter diamond[i] += letter[i]; }
// to draw the first section of the diamond
System.out.println(diamond[i]); } for (int i = letter_number – 1; i >= 0; i–)
// to draw the second part of the diamond
// to write the diamondArray in reverse order
System.out.println(diamond[i]);
} catch (Exception e) {
e.printStackTrace();
} finally {
reader.close();
B B
C C
D D
E E
F F
for (int i=rows; i>= 1 ; i–)
for (int j = i; j < rows ; j++) {
for (int k = 1; k <= (2*i -1) ;k++) {
if( k==1 || i == rows || k==(2*i-1)) {
// To print alphabet A pattern
void display(int n)
// an outer for loop for number of lines
for (int i = 0; i<=n; i++) {
// an inner for loop for logic execution
for (int j = 0; j<= n / 2; j++) {
// to print two column lines
if ((j == 0 || j == n / 2) && i != 0 ||
// to print first line of alphabet
i == 0 && j != n / 2 ||
// to print middle line
i == n / 2)
JigSawAcademy a = new JigSawAcademy();
a.display(7);
Scanner sc = new Scanner(System.in); // rows value from the user
for (int i = 1; i <= rows; i++)
for (int j = 1; j <= i; j++)
System.out.print(i+” “);
2 2
3 3 3
4 4 4 4
5 5 5 5 5
if(j%2 == 0)
System.out.print(0);
System.out.print(1);
10
101
1010
10101
int num;
if(i%2 == 0)
num = 0;
for (int j = 1; j <= rows; j++)
System.out.print(num);
num = (num == 0)? 1 : 0;
num = 1;
01010
//take a row-value from the user
System.out.println(“How many rows do you want in this pattern?”);
System.out.println(“Here is your pattern….!!!”);
for (int j = rows; j >= i; j–)
System.out.print(j+” “);
//to close all the resources
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
class JigSawAcademy
//to print the pattern
static void printPattern(int n)
// the number of rows & columns to print
int s = 2 * n – 1;
// upper half of matrix
for (int i = 0;
i < (s / 2) + 1; i++)
int m = n;
// the decreasing part
for (int j = 0; j < i; j++)
System.out.print(m + ” “);
m–;
// the constant part
for (int k = 0;
k < s – 2 * i; k++)
System.out.print(n – i + ” “);
// the increasing part.
m = n – i + 1;
for (int l = 0; l < i; l++)
m++;
// lower half of the matrix
for (int i = s / 2 – 1;
i >= 0; i–)
// the constant part.
// the driver code
public static void main (String[] args)
printPattern(n);
3 3 3 3 3
3 2 2 2 3
3 2 1 2 3
This blog is a collection of different pattern programs in java; From basic pattern programs in java to difficult pattern programs in java. If you wish to learn more about the various aspects of Java programming, check out our UNext page for other blogs regarding the same.
Fill in the details to know more
What Is the Use of Wrapper Class in Java?
March 22, 2023
What Is Clean Coding in Java?
March 21, 2023
What Are the New Features of Java 17?
What Is File Handling in Java?
March 16, 2023
What Is Data and Time Function in Java?
March 11, 2023
Top 10 Emerging Technologies Blogs To Read In 2023
December 15, 2022
History of Java Programming Language| UNext
November 18, 2022
An Advanced Guide to Shell Scripting in Unix!
November 15, 2022
Java Tutorial For Beginners
September 28, 2022
What Is Data Abstraction?
August 24, 2022
Encapsulation In Oops With Example
August 23, 2022
Add your details:
By proceeding, you agree to our privacy policy and also agree to receive information from UNext through WhatsApp & other means of communication.
Upgrade your inbox with our curated newletters once every month. We appreciate your support and will make sure to keep your subscription worthwhile