Java Program to Create Pyramid and Pattern



If someone wants to build a strong foundation in Java programming language. Then, it is necessary to understand how loops work. Also, solving the pyramid pattern problems is the best way to enhance the fundamentals of Java as it includes extensive use of ?

This article aims to provide a few Java programs to print pyramid patterns with the help of different types of loops available in Java.

Java program to create pyramid patterns

We are going to print the following pyramid patterns through Java programs ?

Let's discuss them one by one.

Pattern 1: Half Star Pyramid

Below are the steps to print half star pyramid ?

  • The program defines a class Pyramid1 with a main method.
  • Inside the main method, an integer rows is initialized to 5, representing the number of rows.
  • A for loop runs from i = 1 to i <= rows, controlling the number of rows.
  • Inside the outer loop, another for loop runs from j = 1 to j <= i, printing * in each row based on the current row number.
  • After printing the stars in each row, a new line is printed.
  • The process repeats until the pyramid is printed with increasing stars in each row.

Example

public class Pyramid1 {
    public static void main(String[] args) {
        int rows = 5;
        for (int i = 1; i <= rows; ++i) {
            for (int j = 1; j <= i; ++j) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Output

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

Pattern 2: Inverted Half Star Pyramid

Below are the steps to print inverted half star pyramid ?

  • The program defines a class Pyramid2 with a main method.
  • Inside the main method, an integer rows is initialized to 6, representing the number of rows.
  • A for loop runs from i = rows (starting at 6) and decrements until i >= 1, controlling the number of rows in reverse order.
  • Inside the outer loop, a second for loop runs from j = 1 to j <= i, printing * based on the current value of i.
  • After printing the stars for each row, a new line is printed.
  • The process continues, printing a decreasing number of stars in each row until the pattern is completed.

Example

public class Pyramid2 {
  public static void main(String[] args) {
    int rows = 6;
    for (int i = rows; i >= 1; --i) {
      for (int j = 1; j <= i; ++j) {
        System.out.print("* ");
      }
      System.out.println();
    }
  }
}

Output

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

Pattern 3: Star Pyramid

Below are the steps to print star pyramid ?

  • Declare and initialize an integer 'n' that specifies the number of rows.

  • Create a nested for loop, the outer one will run till 'n' and the inner for loop will run till the number of spaces and print the spaces. After printing, we will decrease the space count by 1.

  • Again take another inner for loop that will run till a number of stars and print the stars. After printing, we will increment the star count by 2.

Example

public class Pyramid3 {
   public static void main(String[] args) {
      int n = 5; // number of rows
      int spc = n-1; // initial space count
      int str = 1; // initial star count
      // loop to print the pyramid
      for(int i = 1; i <= n; i++) {
         for(int j = 1; j <= spc; j++) { // spaces
            System.out.print("\t"); 
         }
         spc--; // decrementing spaces
         for(int k = 1; k <= str; k++) { // stars
            System.out.print("*\t");  
         }
         str += 2; // incrementing stars
         System.out.println();
      }
   }
}

Output

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

Pattern 4: Inverted Star Pyramid

Below are the steps to print inverted star pyramid ?

  • Declare and initialize an integer 'n' that specifies the number of rows.
  • Next, define the initial count of space as 0 and the initial star count as 'n + n - 1' so that we can maintain the number of columns as odd.
  • Create a nested for loop, the outer one will run till 'n' and the first inner for loop will print the spaces. After printing, we will increment the space count by 1 with each iteration
  • Again take another inner for loop that will print the stars. After printing, we will decrement the star count by 2.

Example

public class Pyramid4 {
   public static void main(String[] args) {
      int n = 5;
      int spc = 0; // initial space count
      int str = n + n - 1; // initial star count
      // loop to print the star pyramid
      for(int i = 1; i <= n; i++) {
         for(int j = 1; j <= spc; j++) { // spaces
            System.out.print("\t"); 
         }
         spc++; // incrementing spaces
         for(int k = 1; k <= str; k++) { // stars
            System.out.print("*\t");  
         }
         str -= 2; // decrementing stars
         System.out.println();
      }
   }
}

Output

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

Pattern 5: Numeric Pyramid

We will use the previous code here, but instead of printing the stars, we will print the column number in each row.

  • First we will define a class Pyramid5 with a main method.
  • Inside the main method, three variables are initialized: n = 5 , spc = n-1, and col = 1.
  • A for loop runs from i = 1 to i <= n to control the number of rows.
  • Inside the loop, a second for loop runs from j = 1 to j <= spc to print spaces.
  • After the space loop, spc is decremented by 1.
  •  Another for loop runs from k = 1 to k <= col to print numbers.
  • After the number loop, col is incremented by 2 and a new line is printed
  • The process continues until the pyramid pattern is printed with increasing numbers and decreasing spaces.

Example

public class Pyramid5 {
   public static void main(String[] args) {
      int n = 5; // number of rows
      int spc = n-1; // initial space count
      int col = 1; // initial column count
      // loop to print the pyramid
      for(int i = 1; i <= n; i++) {
         for(int j = 1; j <= spc; j++) { // spaces
            System.out.print("\t"); 
         }
         spc--; // decrementing spaces
         for(int k = 1; k <= col; k++) { // numbers
            System.out.print(k + "\t");  
         }
         col += 2; // incrementing the column
         System.out.println();
      }
   }
}

Output

            1	
	 1  2  3	
      1  2  3  4  5	
   1  2  3  4  5  6  7	
1  2  3  4  5  6  7  8  9

Conclusion

In this article, we have discussed five Java programs to print pyramid patterns. These pattern solutions will help us to decode the logic of the pattern problems and make us capable of solving other patterns on our own. To solve patterns like these, we use the loops and conditional blocks.

Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2024-12-24T15:36:48+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements