How to Print Pyramid Pattern in Java? Program Example | Java Pattern Programming

Pattern based exercises are a good way to learn nested loops in Java. There are many pattern based exercises and one of them is printing Pyramid structure as shown below:


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

You need to write a Java program to print the above pyramid pattern. How many levels the pyramid triangle would have will be decided by the user input. You can print this kind of pattern by using print() and println() method from System.out object. System.out.print() just prints the String or character you passed to it, without adding a new line, useful to print stars in the same line. 
While, System.out.println() print characters followed by a newline character, which is useful to move to the next line. You can also use the Scanner class to get input from the user and draw a pyramid up to that level only. For example in the above diagram, the pyramid has 5 levels.

Analysis

If you look at the problem then you will find that you need to print the star (*) character in the same line as well as a new line to generate a pyramidical pattern. You can also see that * are separated by space. In programming, to do a task repeatedly e.g. printing star, you can use a loop. 
This kind of problem, which require printing in row and column usually require two loops, one inside another. Also, known as nested loops. You can use for() loop to create this pattern as shown below:

 public static void drawPyramidPattern() {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5 - i; j++) {
                System.out.print(" ");
            }
            for (int k = 0; k <= i; k++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }

There are three loops nested at two level, first is for printing each line and inner loops for printing pattern in each line.

Java Program to Print Pyramid Pattern

Here is our Java program to draw the pyramid pattern as shown in the problem statement. In this program, we have two examples of printing pyramids, in the first, we have a printed pyramid of star characters, while, in the second example, we have drawn a pyramid of numbers. 
The key here is to use both print() and println() method from PrintStream class, which is also easily accessible as System.out object. We have also used nested for loop to draw the pyramid which you will often use to solve this kind of problem.

How to print Pyramid Pattern in Java with example

Sample code in Java to Print the Pyramid Pattern

import java.util.Scanner;

/**
 * Simple Java Program to draw a pyramid pattern. We have used both
 * System.out.println() and System.out.print() methods to draw stars(*)
 * in pyramid shape.
 * 
 * @author WINDOWS 8
 *
 */
public class PrintPyramidTest {

    public static void main(String args[]) {
        System.out.println("Pyramid pattern of star in Java : ");
        drawPyramidPattern();
        
        System.out.println("Pyramid of numbers in Java : ");
        drawPyramidOfNumbers();
    }

    /**
     * This method draws a pyramid pattern using asterisk character. You can
     * replace the asterisk with any other character to draw a pyramid of that.
     */
    public static void drawPyramidPattern() {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5 - i; j++) {
                System.out.print(" ");
            }
            for (int k = 0; k <= i; k++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
    
    
    /**
     * This method draws a pyramid of numbers. 
     */
    public static void drawPyramidOfNumbers() {
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5 - i; j++) {
                System.out.print(" ");
            }
            for (int k = 0; k <= i; k++) {
                System.out.print(k + " ");
            }
            System.out.println();
        }
    }
}

Output :
Pyramid pattern of star in Java : 
     * 
    * * 
   * * * 
  * * * * 
 * * * * * 
Pyramid of numbers in Java : 
     0 
    0 1 
   0 1 2 
  0 1 2 3 
 0 1 2 3 4 

That’s all about how to print Pyramid using Java pattern. You can find many pattern printing exercises in Java or C++ programming books. You can further refine this program to print any other character instead of * or you can ask the user to enter the number of rows. You can even modify this program to print a pyramid of numbers.

1 thought on “How to Print Pyramid Pattern in Java? Program Example | Java Pattern Programming”

  1. Pingback: JAVA TOP 50 PROGRAMMING QUESTIONS WITH SOLUTIONS - GRAD JOB OPENINGS

Leave a Comment

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

error: Content is protected !!