Java Program To Print Pyramid
JAVA Programming Language Java Loops

Java Program To Print Pyramid

We are back with another example of loops in java. In this article, we are going to teach you how to write a Java Program To Print Pyramid. So, let’s start creating the program.

Java Program To Print Pyramid:

In this section, we are discussing the logic of the program to print the pyramid pattern in java. Simply, take the number of rows from the user and used 3 for loops one outer loop, and two inner loops to print this pattern. The first loop iterates from 1 to the number of rows entered by the user and we have to print the spaces. The logic to print the spaces is we entered 7 rows and in the output, we clearly see that there are 6 spaces and 1 star in the first row.

In the second row, the star will increase from 1 to 2 and the number of spaces will decrease from 6 to 5 and it iterates until the spaces beomes zero. It means in every iteration the first loop print the spaces (n-i) and in the second inner loop we will print the star and a space. This time the space is used to print the pyramid neatly and clearly to the user.

Source Code:

import java.util.*;
public class Pyramid {
    public static void main(String[] args) {
        Scanner sc = new Scanner(;
        System.out.print("Enter The Number Of Rows : ");
        int n = sc.nextInt();
        for(int i =1; i<n ; i++){
            for(int j=1 ; j<n-i ; j++){
                System.out.print(" ");
            for(int j =1 ; j<=i ;j ++){
                System.out.print("* ");


Java Program To Print Pyramid
Java Program To Print Pyramid





We recently published an article in which all difficult patterns are provided. Click here to direct to the article. If any of the patterns you are looking for is not provided by us then tell us in the comments. We will upload as soon as possible

First Video Link: Advanced Patterns In Java

Second Video Link:

Leave a Reply

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