Pattern printing is a common problem used to understand nested loops, recursion and control flow in Java. In this article, we explore multiple Java programs to print pyramid, triangle, number and special patterns using different approaches such as for-loops, while-loops, and recursion.
1. Simple Star Pyramid Pattern
Using Nested for Loops:
- Use an outer loop to control the number of rows.
- Use an inner loop to print stars in each row.
- Print a new line after completing each row.
class GFG {
static void printStars(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
public static void main(String[] args) {
int n = 5;
printStars(n);
}
}
Output
* * * * * * * * * * * * * * *
2. Simple Pyramid Using while Loop
Iterative Pattern Printing:
- Use one while loop for rows.
- Use a nested while loop to print stars per row.
- Reset column counter after each row.
class GFG {
public static void main(String[] args) {
int r = 1, c = 0, n = 5;
while (r <= n) {
while (c < r) {
System.out.print("* ");
c++;
}
r++;
c = 0;
System.out.println();
}
}
}
3. Simple Pyramid Using Recursion
Recursive Row and Pattern Printing:
- Use one recursive method to print stars in a row.
- Use another recursive method to manage rows.
- Increase stars count after each recursive call.
class GFG {
static void printRow(int count) {
if (count == 0) return;
System.out.print("* ");
printRow(count - 1);
}
static void pattern(int rows, int current) {
if (rows == 0) return;
printRow(current);
System.out.println();
pattern(rows - 1, current + 1);
}
public static void main(String[] args) {
pattern(5, 1);
}
}
Output
* * * * * * * * * * * * * * *
4. Right-Aligned Pyramid (180° Rotated)
Using Spaces and Stars:
- Print leading spaces to shift stars to the right.
- Print stars after spaces.
- Reduce spaces and increase stars per row.
class GFG {
static void printStars(int n) {
for (int i = 0; i < n; i++) {
for (int j = 2 * (n - i); j >= 0; j--) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
public static void main(String[] args) {
printStars(5);
}
}
Output
*
* *
* * *
* * * *
* * * * *
5. Triangle Pattern
- Print decreasing spaces for alignment.
- Print stars in increasing order.
- Move to next line after each row.
class GFG {
static void printTriangle(int n) {
for (int i = 0; i < n; i++) {
for (int j = n - i; j > 1; j--) {
System.out.print(" ");
}
for (int j = 0; j <= i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
public static void main(String[] args) {
printTriangle(5);
}
}
Output
* * * * * * * * * * * * * * *
6. Number Pyramid Pattern
Resetting Number for Each Row:
- Reset the number to 1 at the start of each row.
- Print numbers sequentially per row.
- Increase row size gradually.
class GFG {
static void printNums(int n) {
for (int i = 0; i < n; i++) {
int num = 1;
for (int j = 0; j <= i; j++) {
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
public static void main(String[] args) {
printNums(5);
}
}
Output
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
7. Continuous Number Pyramid (Without Reset)
- Initialize a number before starting loops.
- Print numbers continuously without resetting.
- Increment the number after each print.
class GFG {
static void printNums(int n) {
int num = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(num + " ");
num++;
}
System.out.println();
}
}
public static void main(String[] args) {
printNums(5);
}
}
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
8. Christmas Tree Pattern Using Pyramid
- Create multiple pyramid layers with increasing width.
- Adjust spaces to center each layer.
- Print a rectangular trunk at the bottom.
class PrintChristmasTree {
public static final int HEIGHT = 5;
public static void main(String[] args) {
int width = 5;
int space = width * 5;
int x = 1;
for (int a = 1; a <= HEIGHT; a++) {
for (int i = x; i <= width; i++) {
for (int j = space; j >= i; j--) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print("* ");
}
System.out.println();
}
x += 2;
width += 2;
}
// Tree trunk
for (int i = 1; i <= 4; i++) {
for (int j = space - 3; j >= 1; j--) {
System.out.print(" ");
}
for (int k = 1; k <= 4; k++) {
System.out.print("* ");
}
System.out.println();
}
}
}
Output:
** *
* * *
* * * *
* * * * *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
* * * * * * * * * * * * *
* * * *
* * * *
* * * *
* * * *