Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Pyramid Program in Java

In this article, we will learn what is pyramid program, how to create a half pyramid program in java, and a full pyramid program in java. We will also understand pyramid programs in java with the help of an example.

What is a pyramid program?

A pattern program with a pyramid shape is known as a pyramid program. Multiple for-loops and print statements are used to create the pattern.
Below are some images of the pyramid pattern.

The above image is an example of a full pyramid pattern using the symbol *. To create this kind of full pyramid pattern generally, we have to use more than two ‘for’ loops. The number of ‘for’ loops depends on the pattern of the pyramid.

The above images are examples of half-pyramid patterns using the numbers and alphabet. To create this kind of pattern generally, we have to use two ‘for’ loops.

Examples of the half-pyramid program in java

Example 1: Pyramid program in java to print half pyramid using the symbol *.

/* Pyramid program in java to print half pyramid using *
*/
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
 
public class Main
{
    public static void main (String[] args)
    {
        int i,j,rows;
        Scanner my_scanner = new Scanner(System.in);
 
        System.out.println("Enter the number of rows to print * =");
        rows = my_scanner.nextInt();
 
 
 
    for (i = 1; i <= rows; i++) 
    {
      for ( j = 1; j <= i; j++)
      {
        System.out.print("* ");
      }
      System.out.println();
    }
    }
}

Output:

Enter the number of rows to print * = 5
* 
* * 
* * * 
* * * * 
* * * * * 

In the above pyramid program in java, first, we will take the input from the user for the number of rows. After that, we will run an outer for loop from 1 to rows and we will run an inner for loop from 1 to i where i is the current row number. During each inner for loop we will print one and after completion of the row, we will move to another row. In the output, we can see that in the first line one is printed and the number of * increased by 1 in every upcoming row.

Example 2: Pyramid program in java to print half pyramid using the numbers.

/* Pyramid program in java to print half pyramid using the numbers
*/
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
 
public class Main
{
    public static void main (String[] args)
    {
        int i,j,rows;
        Scanner my_scanner = new Scanner(System.in);
 
        System.out.println("Enter the number of rows to print numbers =");
        rows = my_scanner.nextInt();
 
 
 
    for (i = 1; i <= rows; i++) 
    {
      for ( j = 1; j <= i; j++)
      {
        System.out.print(j + " ");
      }
      System.out.println();
    }
    }
}

Output:

Enter the number of rows to print numbers = 5
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5

In the above pyramid program in java, first, we will take the input from the user for the number of rows. After that, we will run an outer for loop from 1 to rows and we will run an inner for loop from 1 to i where i is the current row number. During each inner for loop, we will print the value of j and after completion of the row, we will move to another row. In the output, we can see that in the first line the value of j (1) is printed and the value of j increased by 1 in every upcoming row.

Example 3: Pyramid program in java to print half pyramid using alphabets.

/* Pyramid program in java to print half pyramid using alphabets
*/
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
 
public class Main
{
    public static void main (String[] args)
    {
        int i,j,rows;
        char alpha= 'A';
        Scanner my_scanner = new Scanner(System.in);
 
        System.out.println("Enter the number of rows to print alphabets =");
        rows = my_scanner.nextInt();
 
 
 
    for (i = 1; i <= rows; i++) 
    {
    	
      for ( j = 1; j <= i; j++)
      {
        System.out.print(alpha + " ");
      }
      System.out.println();
      alpha++;
    }
    
    }
}

Output:

Enter the number of rows to print alphabets = 5
A 
B B 
C C C 
D D D D 
E E E E E

In the above pyramid program in java, first, we will take the input from the user for the number of rows. After that, we will run an outer for loop from 1 to rows and we will run an inner for loop from 1 to i where i is the current row number. During each inner for loop, we will print the value of variable alpha where alpha is the ASCII value of character ‘A’ and after completion of the row, we will move to another row and increment the value of variable alpha by 1. In the output, we can see that in the first line character A is printed and the number of characters increased by 1 in every upcoming row.

Algorithm for the half-pyramid program in java:

Step 1: First, we will declare three variables of integer data type: i, j, and rows. Variable i indicates the number of rows, variable j indicates the number of columns, and rows is used to take input from the user.

Step 2: Then, We will take the value from the user to print the number of rows to print using Scanner and store it in the rows variable.

Step 3: We will write two ‘for’ loops. The first ‘for’ loop iterates the number of rows, while the second ‘for’ loop iterates the number of columns. The symbol * is then printed based on the number of columns. We will write a print statement in the second loop for printing symbols or characters or numbers.

Examples of full pyramid program in java

Example 1: Pyramid program in java to print full pyramid using the symbol *.

/* Pyramid program in java to print full pyramid using the symbol *
*/
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
 
public class Main
{
    public static void main (String[] args)
    {
        int i,j,k,rows;
        Scanner my_scanner = new Scanner(System.in);
 
        System.out.println("Enter the number of rows to print symbol * =");
        rows = my_scanner.nextInt();
 
 
 
   for (i = 1; i <= rows; i++) {
      for (j=rows-i; j>=1; j--)
            {
              
                System.out.print(" ");
            }
    
    for (k=1; k<=i; k++ )
            {
               
                System.out.print("* ");
            }

      System.out.println();
    }
    }
}

Output:

Enter the number of rows to print symbol * = 5
    * 
   * * 
  * * * 
 * * * * 
* * * * *

In the above pyramid program in java, first, we will take the input from the user for the number of rows. After that, we will run an outer for loop from 1 to rows and we will run the first inner for loop from j=rows-1 to j>=1 where j is the current column number. In short, the value of j is decreased by 1 at every iteration thus the number of white spaces will reduce in every row. Then we will run the second inner for loop from 1 to i where i is the current row number. During each second inner for loop we will print one and after completion of the row, we will move to another row. In the output, we can see that in the first line one is printed and the number of * increased by 1 in every upcoming row.

Example 2: Pyramid program in java to print full pyramid using the numbers.

/* Pyramid program in java to print full pyramid using the numbers
*/
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
 
public class Main
{
    public static void main (String[] args)
    {
        int i,j,k,rows;
        Scanner my_scanner = new Scanner(System.in);
 
        System.out.println("Enter the number of rows to print numbers =");
        rows = my_scanner.nextInt();
 
 
 
   for (i = 1; i <= rows; i++) {
      for (j=rows-i; j>=1; j--)
            {
              
                System.out.print(" ");
            }
    
    for (k=1; k<=i; k++ )
            {
               
                System.out.print( i +" ");
            }

      System.out.println();
    }
    }
}

Output:

Enter the number of rows to print numbers =
    1 
   2 2 
  3 3 3 
 4 4 4 4 
5 5 5 5 5 

In the above pyramid program in java, first, we will take the input from the user for the number of rows. After that, we will run an outer for loop from 1 to rows and we will run the first inner for loop from j=rows-1 to j>=1 where j is the current column number. In short, the value of j is decreased by 1 at every iteration thus the number of white spaces will reduce in every row. Then we will run the second inner for loop from 1 to i where i is the current row number. During each second inner for loop, we will print the value of variable i where i is the current row number and after completion of the row, we will move to another row. In the output, we can see that in the first line the value of i (1) is printed and the value of i increased by 1 in every upcoming row.

Algorithm for the full pyramid program in java:

Step 1: First, we will declare a number of variables of integer data type according to the pattern of the pyramid. For example i, j,k, and rows. Variable i indicates the number of rows, variable j and k indicate the number of columns, and rows is used to take input from the user.

Step 2: Then, We will take the value from the user to print the number of rows to print using Scanner and store it in the rows variable.

Step 3: We will write more than two ‘for’ loops according to the pattern of the pyramid. The outer ‘for’ loop iterates the number of rows, while other inner loops iterate the number of columns as per requirement. The symbol, character, and numbers are then printed based on the number of columns. We will write a print statement in the second loop for printing symbols or characters or numbers.

Leave a Reply

Your email address will not be published. Required fields are marked *