Pattern printing is a great way to improve logic-building skills in Java. It's widely used in coding interviews, competitive programming, and as a fundamental exercise to understand loops and conditional structures.
In this blog, we'll explore some of the most common and interesting pattern problems, with explanations and Java code implementations.
Square Pattern:
A square pattern is one of the simplest patterns, where the same character or number is printed in a grid format. The key idea behind this pattern is using two nested loops:
- The outer loop controls the rows.
- The inner loop controls the columns and prints the character in each row.
This is useful for understanding the basics of nested loops.
import java.util.Scanner; | |
public class SquarePattern { | |
void pattern(int n) { | |
for (int i = 1; i <= n; i++) { | |
for (int j = 1; j <= n; j++) { | |
System.out.print("* "); | |
} | |
System.out.println(); | |
} | |
} | |
public static void main(String[] args) { | |
int n; | |
Scanner sc = new Scanner(System.in); | |
System.out.print("Enter the number of rows and columns : "); | |
n = sc.nextInt(); | |
SquarePattern p = new SquarePattern(); | |
p.pattern(n); | |
} | |
} |

Right-Angled Triangle Pattern:
This pattern builds a right-angled triangle using increasing rows of symbols.
- The number of symbols in each row is equal to the row index.
- The outer loop runs for the total number of rows, while the inner loop prints the increasing count of symbols.
It helps in understanding incremental loop structures.
import java.util.Scanner; | |
public class RightAngledTriangle { | |
void pattern(int n) { | |
for (int i = 1; i <= n; i++) { | |
for (int j = 1; j <= i; j++) { | |
System.out.print("* "); | |
} | |
System.out.println(); | |
} | |
} | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Enter the number of rows in a triangle: "); | |
int n = sc.nextInt(); | |
RightAngledTriangle p = new RightAngledTriangle(); | |
p.pattern(n); | |
} | |
} |
Inverted Pyramid Pattern:
n this pattern, we start with the maximum number of symbols in the first row, decreasing in each subsequent row.
- The outer loop runs from the maximum row count down to 1.
- The inner loop ensures that each row has fewer symbols than the previous one.
It helps in practicing decremental looping techniques.
import java.util.Scanner; | |
class InvertedPyramid { | |
void pattern(int n) { | |
for (int i = 0; i < n; i++) { | |
for (int j = n; j > i; j--) { | |
System.out.print("* "); | |
} | |
System.out.println(); | |
} | |
} | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Enter the number of rows : "); | |
int n = sc.nextInt(); | |
InvertedPyramid pyramid = new InvertedPyramid(); | |
pyramid.pattern(n); | |
} | |
} |
Right-Angled Number Pyramid 1:
This pattern is a variation of the right-angled triangle but uses numbers instead of symbols.
- The numbers increase with each row, providing an easy way to understand number-based pattern printing.
It's helpful for working with number sequences in nested loops.
import java.util.Scanner; | |
class RightAngledNumberPyramid { | |
void pattern(int n) { | |
for (int i = 1; i <= n; i++) { | |
for (int j = 1; j <= i; j++) { | |
System.out.print(j + " "); | |
} | |
System.out.println(); | |
} | |
} | |
public static void main(String[] args) { | |
int n; | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Enter the number of rows in a pyramid: "); | |
n = sc.nextInt(); | |
RightAngledNumberPyramid r = new RightAngledNumberPyramid(); | |
r.pattern(n); | |
} | |
} |
Right-Angled Number Pyramid 2:
This is another numeric pyramid but prints numbers in increasing order, forming a right-angled shape.
- The outer loop controls the number of rows.
- The inner loop ensures that numbers in each row increase progressively.
This enhances logical understanding of number-based patterns.
import java.util.Scanner; | |
class RightAngledNumberPyramid2 { | |
public void pattern(int n) { | |
for (int i = 1; i <= n; i++) { | |
for (int j = 1; j <= i; j++) { | |
System.out.print(i + " "); | |
} | |
System.out.println(); | |
} | |
} | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Enter the number of rows: "); | |
int n=sc.nextInt(); | |
RightAngledNumberPyramid2 p = new RightAngledNumberPyramid2(); | |
p.pattern(n); | |
} | |
} |
Inverted Number Pyramid:
This pattern is the inverted form of the numeric pyramid.
- The outer loop runs in decreasing order.
- The inner loop prints numbers based on the row index.
It is an excellent way to practice decrementing loops and number manipulation.
import java.util.Scanner; | |
class InvertedNumberPyramid { | |
void pattern(int n) { | |
for (int i = 0; i < n; i++) { | |
for (int j = n; j > i; j--) { | |
System.out.print(n-j+1+" "); | |
} | |
System.out.println(); | |
} | |
} | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
System.out.println("Enter the number of rows : "); | |
int n = sc.nextInt(); | |
InvertedPyramid pyramid = new InvertedPyramid(); | |
pyramid.pattern(n); | |
} | |
} |