12 Pattern Programs in Java That Separate Beginner Coders from the Pros!
By Rohan Vats
Updated on Jun 27, 2025 | 20 min read | 21.88K+ views
Share:
For working professionals
For fresh graduates
More
By Rohan Vats
Updated on Jun 27, 2025 | 20 min read | 21.88K+ views
Share:
Did you know? Pattern programs in Java are a staple in coding interviews for beginners, often taking center stage in entry-level technical assessments. Mastering them could be your ticket to acing that next job interview! |
Pattern programs in Java are algorithms that generate specific shapes or designs using characters or numbers in a structured manner. These programs typically involve nested loops to print patterns like triangles, squares, pyramids, or diamonds.
They are often used to enhance problem-solving and coding skills by teaching the importance of loops, logic, and design thinking. For example, they can be used for designing a seating arrangement in a theater, where each row or seat is represented in a specific pattern to optimize space.
In this blog, you'll explore the concept of pattern programs in Java, learn how to implement them using looping statements, and see real-world examples of their applications.
Want to learn how to use programming tools and techniques efficiently for better outcomes? Join upGrad’s Online Software Development Courses and work on hands-on projects that simulate real industry scenarios.
Before we delve into the various pattern programs 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.
In 2025, developers who can use advanced programming techniques, like pattern programs in Java, will be highly sought after. If you're looking to sharpen your coding skills, check out these top-rated courses to help you build a strong foundation:
Here’s a table with a brief overview of the 12 pattern programs in Java:
Pattern Program |
Where is It Used? |
Simple Triangle Pattern | Used in designing basic layouts or visual representations in console-based apps. |
Double Triangle Pattern | Common in visualizing geometric shapes, used in design and game development. |
Triangle Pattern with Repeated Digits | Applied in educational tools for number theory and patterns in programming. |
Inverted Triangle with Descending Digits | Often used in puzzles, games, and challenges requiring descending patterns. |
Triangle with Repeating Pattern | Useful in visualizing patterns in UI/UX, graphical design, or simulations. |
Double Triangle Pattern | Employed in graphic design, educational programs, and technical diagramming. |
Inverted Double Triangles | Found in pattern recognition tasks or used in creative designs in coding challenges. |
Double Inverted Triangles | Used in artistic visualizations, game designs, and challenges for practicing loops. |
Digit Pillar Pattern | Often used in numeric pattern exercises or educational apps focusing on mathematics. |
Binary Digit Pattern | Common in learning binary systems, logic design, or algorithm teaching. |
Ascending Triangle Pattern | Frequently used in sorting algorithms, design pattern visualization, and learning exercises. |
Square Java Pattern Program | Used in basic shape rendering, graphical simulations, and learning algorithms. |
You can get a better hang of pattern programs in Java with upGrad’s free Core Java Basics course. It covers variables, data types, loops, and OOP principles to build strong coding skills. Perfect for aspiring developers, students, and professionals transitioning to Java.
Also Read: Java Language History: Key Milestones and Development
Next, we’ll explore each of these pattern programs in Java in detail with code examples.
This basic pattern creates a simple triangular shape using stars or numbers. It’s ideal for beginners to practice loops and basic control flow.
Here are some of its key features:
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();
}
}
Explanation: This Java program generates a simple triangle pattern where each row contains numbers starting from 1 up to the current row number. The number of rows is determined by user input. The program uses nested loops, where the outer loop handles the rows, and the inner loop prints the numbers in each row.
Code Output: If the user inputs 5 for the number of rows, the output will be:
How many rows you want in this pattern?
5
Here is your pattern....!!!
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
This pattern combines two triangles facing each other, either using numbers or characters. It helps to visualize symmetry in programming.
Here are some of its key features:
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();
}
}
Explanation: This Java program generates a diamond-shaped number pattern. The user is prompted to input the number of rows for the top half of the pattern. The program then prints the upper half of the pattern by incrementing numbers, followed by the lower half, which is the reverse of the upper half. The program uses two separate loops: one for the upper half and one for the lower half.
Code Output: If the user inputs 5 for the number of rows, the output will be:
How many rows you want in this pattern?
5
Here is your pattern....!!!
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Are you a Java developer wanting to integrate AI into your workflow? upGrad’s AI-Driven Full-Stack Development bootcamp can help you. You’ll learn how to build AI-powered software using OpenAI, GitHub Copilot, Bolt AI & more.
Learn more: Top 8 Java Projects on Github You Should Get Your Hands-on
upGrad’s Exclusive Software and Tech Webinar for you –
SAAS Business – What is So Different?
This program generates a triangle pattern using the same digit repeatedly in each row. It’s used to practice loops and output formatting.
Here are some of its key features:
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();
}
}
Explanation: This Java program generates a pattern where each row contains the same number, which is equal to the row number itself. The user is prompted to input the number of rows, and the program uses nested loops to print the pattern. The outer loop handles the rows, and the inner loop prints the same number (the row number) for each column in that row.
Code Output: If the user inputs 5 for the number of rows, the output will be:
How many rows you want in this pattern?
5
Here is your pattern....!!!
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
This pattern creates an inverted triangle where numbers decrease with each row. It’s typically used in number theory exercises.
Here are some of its key features:
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();
}
}
Explanation: This Java program generates a pattern where each row contains numbers that start from the total number of rows and decrease with each iteration. The user is prompted to input the number of rows, and the program uses nested loops to print the numbers in a descending order for each row. The outer loop controls the rows, while the inner loop handles the printing of numbers in decreasing order.
Code Output: If the user inputs 5 for the number of rows, the output will be:
How many rows you want in this pattern?
5
Here is your pattern....!!!
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
Also Read: Top 10 Free Java Courses with Certificates for In-Demand Java Jobs
This pattern creates a triangle with a repeating design in each row, often using numbers or symbols. It’s useful for design pattern exercises.
Here are some of its key features:
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();
}
}
Explanation: This Java program generates a pattern where each row first increases from 1 to the current row number and then decreases back to 1. The user is prompted to input the number of rows, and the program uses two nested loops: the first loop prints the ascending numbers, and the second loop prints the descending numbers for each row. The outer loop controls the rows, while the inner loops handle the printing of the numbers in ascending and descending order.
Code Output: If the user inputs 5 for the number of rows, the output will be:
How many rows you want in this pattern?
5
Here is your 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
This pattern combines two symmetrical triangles, either aligned or staggered. It’s used in algorithm challenges and design tasks.
Here are some of its key features:
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();
}
}
Explanation: This Java program generates a symmetrical pattern where each row starts by printing numbers in ascending order from 1 to the current row number and then prints the same numbers in descending order back to 1. The number of rows is determined by user input. The program uses two nested loops: the first loop prints the ascending numbers, and the second loop prints the descending numbers for each row. The outer loop controls the rows, while the inner loops handle the number printing.
Code Output: If the user inputs 5 for the number of rows, the output will be:
How many rows you want in this pattern?
5
Here is your 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
Also Read: Top Java Courses for 2025 – Developer Approved Picks
This pattern mirrors the double triangle pattern, but inverted. It's often used to challenge problem-solving skills and logic.
Here are some of its key features:
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();
}
}
Explanation: This Java program generates a diamond-like pattern using numbers. The user is prompted to input the number of rows for the upper half of the diamond. The program uses nested loops to first print spaces and then print numbers, starting from the row index to the total number of rows. After printing the upper half, the program prints the lower half of the pattern, which mirrors the upper half. The pattern is symmetric and involves controlling spaces and numbers within each row.
Code Output: If the user inputs 5 for the number of rows, the output will be:
How many rows you want in this pattern?
5
Here is your pattern....!!!
12345
12345
12345
12345
12345
12345
12345
12345
12345
Learn JavaScript from upGrad’s free JavaScript Basics from Scratch course. It covers variables, data types, loops, functions, and event handling. Build interactive web applications and gain the skills to create websites from scratch.
This pattern displays two inverted triangles side by side, often used to create complex visual shapes.
Here are some of its key features:
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();
}
}
Explanation: This Java program generates a diamond-shaped number pattern with spaces. The user is prompted to input the number of rows for the top half of the pattern. The program uses nested loops to print spaces at the beginning of each row, followed by numbers that increment from the current row number to the total number of rows. After printing the upper half, it mirrors the upper half to print the lower half of the diamond. The pattern creates a symmetric, visually appealing design.
Code Output: If the user inputs 5 for the number of rows, the output will be:
How many rows you want in this pattern?
5
Here is your pattern....!!!
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
Also Read: 50 Java Projects With Source Code in 2025: From Beginner to Advanced
The digit pillar pattern is a column of digits, often increasing or decreasing in value. It is commonly used in teaching loops and iteration.
Here are some of its key features:
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();
}
}
Explanation: This Java program generates a pattern where the first half consists of 1's printed for the first part of the row, followed by the row number (i) printed for the second part of the row. The user inputs the number of rows, and the program uses nested loops to print the pattern. The outer loop controls the rows, the first inner loop prints 1 for the required number of spaces, and the second inner loop prints the row number starting from 1 up to the total number of rows.
Code Output: If the user inputs 5 for the number of rows, the output will be:
How many rows you want in this pattern?
5
Here is your pattern....!!!
11111
11112
11123
11234
12345
This pattern generates a sequence of binary digits (0 and 1), often used to teach binary operations and logic.
Here are some of its key features:
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();
}
}
Explanation: This Java program generates a pattern where each row alternates between 1's and 0's. The pattern starts with 1 in the first row, and the numbers alternate between 1 and 0 in each column. For even-numbered rows, it starts with 0, and for odd-numbered rows, it starts with 1. The user inputs the number of rows, and the program uses nested loops to print the alternating numbers for each row.
Code Output: If the user inputs 5 for the number of rows, the output will be:
How many rows you want in this pattern?
5
Here is your pattern....!!!
10101
01010
10101
01010
10101
This triangle starts with one element and gradually increases, often used to show ascending patterns in numerical or symbolic form.
Here are some of its key features:
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();
}
}
Explanation: This Java program generates a pattern where each row starts with the row number and increments by a decreasing value as the columns increase. The user inputs the number of rows, and the program uses two nested loops: the outer loop controls the rows, and the inner loop controls the printing of numbers in each row. The number starts from the row number i and increments by a decreasing value calculated as rows - j.
Code Output: If the user inputs 5 for the number of rows, the output will be:
How many rows you want in this pattern?
5
Here is your pattern....!!!
1
2 6
3 8 12
4 10 15 19
5 11 16 20 23
This pattern creates a square of characters or digits, useful in teaching structured layouts and formatting.
Here are some of its key features:
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();
}
}
Explanation: This Java program generates a pattern where each row contains a sequence of numbers. In the first part of each row, the numbers start from the current row index (i) and increase up to the total number of rows. The second part of each row starts from 1 and goes up to the row index minus 1. This creates a symmetric pattern. The user inputs the number of rows, and the program uses nested loops to print the sequence for each row.
Code Output: If the user inputs 5 for the number of rows, the output will be:
How many rows you want in this pattern?
5
Here is your pattern....!!!
5
4 5 1
3 4 5 1 2
2 3 4 5 1 2 3
1 2 3 4 5 1 2 3 4
Also Read: Python vs Java in 2020: Which One You Should Choose?
Also Read: Careers in Java: How to Make a Successful Career in Java in 2025
Now that you’re familiar with the pattern programs in Java, let’s look at how upGrad can help you learn Java Programming.
Pattern programs in Java are essential in today's job market as it sharpens problem-solving skills and enhances understanding of core programming concepts like loops and arrays. It’s frequently used in technical interviews and coding challenges.
This is where upGrad’s programming courses can help you. They offer expert-led sessions and hands-on exercises to help you master these concepts. With real-time feedback and personalized learning, it prepares you to excel in interviews and build a strong foundation for your software development career.
In addition to the courses covered in the blog, here are some programs to aid in your learning journey:
If you're unsure where to begin or which area to focus on, upGrad’s expert career counselors can guide you based on your goals. You can also visit a nearby upGrad offline center to explore course options, get hands-on experience, and speak directly with mentors!
Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.
Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.
Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.
Reference:
https://www.ccbp.in/blog/articles/pattern-programs-in-java
408 articles published
Software Engineering Manager @ upGrad. Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources