Java Program to Print Pyramid Pattern of Numbers

Java program to print pyramid pattern of numbers. This program will print the number and star together in a single program. It will print the number and star in a single output and also it will print a structure in the program like a pyramid for stars and numbers for outer side of the stars

public class Main
{
public static void main(String[] args)
{
int lines=6;
int i,j,k,l;
int space=0;
for(i=0;i<lines;i++)
{
for(j=1;j<=space;j++)
{
System.out.print(" ");
}
for(j=1;j<=lines;j++)
{
if(j<=(lines-i))
System.out.print(j);
else
System.out.print("*");
}
j--;
while(j>0){
if(j>lines-i)
System.out.print("*");
else
System.out.print(j);
j--;
}
if((lines-i)>10)
space=space+1;
System.out.println("");
}
}
}

 

Output:

123456654321
12345**54321
1234****4321
123******321
12********21
1**********1

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.

Increment Operator in Java

The increment unary operator in Java increases the value of a variable by one, and the decrement unary operator decreases the value of a variable by one. Both update the operand’s value to its new value. The unary operators increment and decrement have two forms: prefix and postfix. 

Increment operators are used to increase the value by one, whereas decrement operates in the opposite direction of increment. Pre-increment I The value is incremented by one before being assigned to the variable. Post-increment (i++) The value is increased after it has been assigned to the variable.

While Loop in Java Program

A while loop in Java is a control flow statement that allows code to be executed several times based on a given Boolean condition. The while loop can be viewed as a looping if statement. A “While” Loop is used to iterate over a certain block of code until a condition is met. 

The condition is assessed first in a while loop, and if it returns true, the statements within the while loop are executed. When the condition returns false, the control exits the loop and moves on to the next line after the while loop.