Create a Half Pyramid Pattern in Java

Exercise:

Write a Java Program to create a half pyramid pattern.

1.Display half pyramid using *

Click Here to View the Solution!
public class HalfPyramid {
  
   public static void main(String[] args) {
     int rows = 7;
  
     for (int i = 1; i <= rows; ++i) {
       for (int j = 1; j <= i; ++j) {
         System.out.print("*");
       }
       System.out.println();
     }
   }
 } 
Click Here to View the Output!
*  
**
***
****
*****
******
*******
Click Here to View the Explanation!
  • In this program, we’re required to print half a pyramid (right side expansion) on the screen using asterisks. The size is predefined for the rows i.e. 7.
  • Note that in this kind of pyramid, the number of asterisks to be printed in a row is equal to the row number.
  • For this purpose, we have two for loops of which one is nested inside the other.
  • The first for loop determines the row number on which the program is currently executing. Thus, the variable ‘i’ represents the row number. Initially, the value of i is 1. After execution of first loop, program enters the second loop.
  • The second loop which is the inner loop, determines the number of asterisks to be printed on a row. It works in such a way that it starts from 1 i.e. j=1 every time the outer loop is iterated and then increments till j is less than or equal to i, which in our case is the current row number.
  • Whenever the inner loop is executed, an ‘* ’ is printed on the screen on row number equal to i.
  • When the value of j exceeds the value of i, the inner loop is broken and a new line is printed, which will be the next row of the pyramid. Then the next iteration of outer loop begins which increments i and it goes on till i is less than or equal to the variable ‘rows’.
  • In this way, a half pyramid is created where every row has the same number of asterisks as the row number with a pre-defined number of rows.

2.Display half pyramid using Numbers

Click Here to View the Solution!
 public class HalfPyramid {
  
   public static void main(String[] args) {
     int rows = 7;
  
     for (int i = 1; i <= rows; ++i) {
       for (int j = 1; j <= i; ++j) {
         System.out.print(j + " ");
       }
       System.out.println();
     }
   }
 } 
Click Here to View the Output!
 1 
 1 2 
 1 2 3 
 1 2 3 4 
 1 2 3 4 5 
 1 2 3 4 5 6 
 1 2 3 4 5 6 7  
Click Here to View the Explanation!
  • In this program half a pyramid (right-side) is created by using numbers i.e. every row will contain digits from 1 up to that specific row number. Example: row three will contain values 1 2 3.
  • The size is predefined for the rows i.e. rows = 7.
  • There are two for loops which determine the placement of these digits.
  • The first for loop determines the row number on which the program is currently executing. Thus, the variable ‘i’ represents the row number. Initially, the value of i is 1. After execution of first loop, program enters the second loop.
  • The second loop which is the inner loop, determines the number of digits to be printed on a row. It works in such a way that it starts from 1 i.e. j=1 every time the outer loop is iterated and then increments till j is less than or equal to i, which in our case is the current row number.
  • So for every iteration of the inner loop, the value of j with space character is printed on the screen.
  • When the value of j exceeds the value of i, the inner loop is broken and a new line is printed, which will be the next row of the pyramid. Then the next iteration of outer loop begins which increments i and it goes on till i is less than or equal to the variable ‘rows’.

3.Display half pyramid using Alphabets

Click Here to View the Solution!
 public class HalfPyramid {
  
   public static void main(String[] args) {
     char last = 'J', alphabet = 'A';
  
     for (int i = 1; i <= (last - 'A' + 1); ++i) {
       for (int j = 1; j <= i; ++j) {
         System.out.print(alphabet + " ");
       }
       ++alphabet;
  
       System.out.println();
     }
   }
 } 
Click Here to View the Output!
 A 
 B B 
 C C C 
 D D D D 
 E E E E E 
 F F F F F F 
 G G G G G G G 
 H H H H H H H H 
 I I I I I I I I I 
 J J J J J J J J J J 
Click Here to View the Explanation!
  • In this program, a half pyramid is created using alphabets. The initial value of the starting alphabet is given i.e. alphabet = A and the last value is also given i.e. last = J. Therefore, it is evident that we need 5 rows for alphabets A-J.
  • The first for loop determines the row number on which the program is currently executing. Thus, the variable ‘i’ represents the row number. Initially, the value of i is 1. After execution of first loop, program enters the second loop.
  • The second loop which is the inner loop, determines the number of alphabets to be printed on a row. It works in such a way that it starts from 1 i.e. j=1 every time the outer loop is iterated and then increments till j is less than or equal to i, which in our case is the current row number.
  • So for every iteration of the inner loop, the value of alphabet with space character is printed on the screen.
  • Note that for increment in a character value, its corresponding Unicode values are used. E.g. A = 65, B = 66 etc.
  • When the value of j exceeds the value of i, the inner loop is broken, the alphabet variable increments by 1, and a new line is printed, which will be the next row of the pyramid. Then the next iteration of outer loop begins which increments i and it goes on till i is less than or equal to (last – ‘A’+1) i.e. (74 – 65 + 1) = 9. Last = J and J= 74.