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

Last Updated on September 22, 2023 by Mayank Dham

Pyramid patterns are a fascinating aspect of programming that showcase the creative potential of code. These patterns, which take on the shape of pyramids, triangles, or other geometric forms, are achieved by skillfully manipulating loops and conditions. In the realm of programming, pyramid patterns not only demonstrate coding prowess but also provide a deep understanding of loop structures and logic. In this article, we will delve into creating pyramid patterns using Java. In this article, we will learn what a pyramid program is, 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.

Conclusion
As we wrap up our exploration of creating pyramid patterns in Java, it’s evident that these exercises go beyond mere aesthetics. They serve as excellent tools for practicing control structures, nested loops, and logical thinking. The ability to break down complex shapes into simple code constructs is a skill that’s transferrable to various programming challenges.

By now, you’ve learned how to create different types of pyramid patterns using varying loop arrangements. As you continue your programming journey, remember that the lessons learned from these exercises can be applied to real-world coding scenarios. The principles of logic, iteration, and pattern recognition are essential skills for any programmer, and mastering them through pyramid pattern exercises is a rewarding endeavor.

FAQ Related to Java pyramid program

Here are some FAQs related to Java Pyramid program.

Q1: Why are pyramid patterns important in programming?
Pyramid patterns are not only visually interesting but also serve as excellent exercises for improving programming skills. They require the application of loop structures and logical thinking, helping programmers enhance their problem-solving abilities.

Q2: Can I use pyramid patterns in practical applications?
While pyramid patterns might not directly relate to most practical applications, the skills you gain from creating these patterns—such as loop manipulation, conditional statements, and pattern recognition—can be applied to a wide range of programming tasks.

Q3: Are pyramid patterns only for beginners?
No, pyramid patterns can be beneficial for programmers at various levels. Beginners can learn the basics of loops and logic, while experienced programmers can use them as creative challenges or even explore more complex pattern formations.

Q4: What types of pyramid patterns can I create in Java?
You can create a variety of pyramid patterns, including full pyramids, half pyramids, inverted pyramids, and more. By manipulating the loop structures and conditions, you can achieve different shapes and designs.

Leave a Reply

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