Java Program to Print Pyramid Pattern of Numbers

Java program to print pyramid pattern of numbers. In the Java programming language, the pyramid number pattern is used. In Java, this program prints numbers pyramid form. Java programming language, print value beginning of the line print pattern.

Pyramid Definition

A pyramid is a polyhedron built by connecting a polygonal base and a point known as the apex. A lateral face is formed by the base edge and apex of each triangle. It has a polygonal basis and is a conic solid. A pyramid has n + 1 vertices, n + 1 faces, and 2n edges with an n-sided base. 

Pyramids are all self-dual. Pyramids were constructed for religious reasons. The Egyptians were among the first to believe in an afterlife. They believed that every human being has a second self known as the ka. When the physical body died, the ka was granted eternal life.

For Loop Definition in Java

For loops are shorthand for while loops in that they increase and test a loop variable. The “for” loop in Java is an entry-controlled loop that allows a user to iteratively run a block of statement(s) for a defined number of times. The number of iterations is determined by the test condition specified within the “for” loop. The Java “for” loop is one of the simplest Java loops to grasp.

A “For” Loop is used to iterate over a specified block of code a predetermined number of times. A for loop is a control flow statement that specifies iteration and allows code to be performed several times. When the number of iterations is known before entering the loop, for loops are commonly employed.

public class Main
{ 
public static void main(String[] args) 
{ 
int n = 5; 
for (int i = 0; i < n; i++) 
{ 
int a = 1; 
System.out.printf("%" + (n - i) * 2 + "s", ""); 
for (int j = 0; j <= i; j++) 
{ 
System.out.printf("%4d", a); 
a = a * (i - j) / (j + 1); 
} 
System.out.println(); 
} 
} 
} 

Output:

                   1    

                 1   1

               1   2   1 

             1   3   3   1  

           1   4   6   4   1