VOOZH about

URL: https://www.geeksforgeeks.org/java/programs-printing-pyramid-patterns-java/

⇱ Programs for printing pyramid patterns in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Programs for printing pyramid patterns in Java

Last Updated : 8 Jan, 2026

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.

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.

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.

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.

Output
 * 
 * * 
 * * * 
 * * * * 
 * * * * * 

5. Triangle Pattern

  • Print decreasing spaces for alignment.
  • Print stars in increasing order.
  • Move to next line after each row.

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.

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.

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.

Output:


  *

* *

* * *

* * * *

* * * * *

* * *

* * * *

* * * * *

* * * * * *

* * * * * * *

* * * * *

* * * * * *

* * * * * * *

* * * * * * * *

* * * * * * * * *

* * * * * * *

* * * * * * * *

* * * * * * * * *

* * * * * * * * * *

* * * * * * * * * * *

* * * * * * * * *

* * * * * * * * * *

* * * * * * * * * * *

* * * * * * * * * * * *

* * * * * * * * * * * * *

* * * *

* * * *

* * * *

* * * *

Comment
Article Tags:
Article Tags: