Pyramid Program in Java
Pyramid Program in Java
In the previous section, we have discussed about the number pattern programs in Java. The logic for the number pattern and pyramid pattern is the same except for some changes. In this section, we will create pyramid programs in Java.
Standard Pyramid Star Pattern
Filename: PyramidPatternExample.java
public class PyramidPatternExample { public static void main(String argvs[]) { int row = 9; // number of rows to be print for (int r = 0; r < row; r++) //handling rows { for (int ind = row - r; ind > 1; ind--) // handling indentation { System.out.print(" "); // printing space } for (int c = 0; c <= r; c++) // loop for columns { System.out.print("* "); // displaying star } System.out.println(); //after printing each row throws the cursor at new line } } }
Output:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Reversed Pyramid Star Pattern
Filename: PyramidPatternExample1.java
public class PyramidPatternExample1 { public static void main(String argvs[]) { int rows = 9; // number of rows to print for (int r = 0; r <= rows - 1; r++) // loop for handling the rows { for (int ind = 0; ind <= r; ind++) // Handling indentation for each row { System.out.print(" "); // Prints spaces } for (int c = 0; c <= rows - 1 - r; c++) // Handling columns { System.out.print("*" + " "); // Prints star and spaces } System.out.println(); //after printing each row throws the cursor at new line } } }
Output:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Right Pyramid Star Pattern
Filename: PyramidPatternExample2.java
public class PyramidPatternExample2 { public static void main(String argvs[]) { int rows = 9; // number of rows to print // logic to print the first half of the pyramid for (int r = 0; r <= rows - 1; r++) { for (int st = 0; st <= r; st++) // Loop for displaying stars { System.out.print("*"+ " "); // Prints stars } System.out.println(""); // Ensuring line break at the end of each row } // logic to print the second half of the pyramid for (int r = rows - 1; r >= 0; r--) { for(int st = 0; st <= r - 1; st++) // Loop for displaying stars { System.out.print("*"+ " "); // Prints stars } System.out.println(""); //after printing each row throws the cursor at new line } } }
Output:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Left Pyramid Star Pattern
Filename: PyramidPatternExample3.java
public class PyramidPatternExample3 { public static void main(String argvs[]) { int rows = 9; // The pyramid will contain 9 rows // logic to print first half pyramid for (int r = 1; r <= rows; r++) // This loop will handle rows { // This inner loop deals with indentation for (int ind = r; ind < rows; ind++) { System.out.print(" "); // Printing spaces } for (int c = 1; c <= r; c++) // Handling columns { System.out.print("*"); // Displaying stars } System.out.println(""); //after printing each row throws the cursor at new line } // logic to print the second half of pyramid for (int r = rows; r >= 1; r--) // loop for rows { for(int ind = r; ind <= rows; ind++) // Handling indentation { System.out.print(" "); // Prints spaces } for(int c = 1; c < r; c++) // Handling columns { System.out.print("*"); // Prints stars } System.out.println(""); // Forcing line break } } }
Output:
* ** *** **** ***** ****** ******* ******** ********* ******** ******* ****** ***** **** *** ** *