This post specifies the code in java language for the particular designed pyramid pattern. These pyramids can be designed with the help of the stars, alphabets & numbers. Each pyramid pattern is designed one by one & according to it, the program is written in java.
Star Pattern Program in Java
There are the following three patterns designed with the help of stars and now to print this pattern the code respective to the pattern is provided in the java language.
Note: In this code For loop with variable (i) is used for rows, variable (J) is used for the column, and variable (k) for spaces.
First: It is a first pattern designed using stars in which as the rows increase the number of stars in the row also increases as if the row is one number of stars are also one vice-versa.
class FirstPattern
{
public static void main(String args[])
{
int i, j,k, row = 5;
for (i=0; i<row; i++)
{
for (k=row-i; k>1; k--)
{
System.out.print(" ");
}
for (j=0; j<=i; j++ )
{
System.out.print("* ");
}
System.out.println();
}
}
}
Second: It is a Second pattern designed using stars in which as the rows increases the number of stars in the row decreases as if the row is one number of stars are also max vice-versa.
class SecondPattern
{
public static void main(String args[])
{
int i, j,k, row = 5;
for (i=0; i<=row-1; i++)
{
for (k=0; k<=i; k++)
{
System.out.print(" ");
}
for (j=0; j<=row-1-i; j++ )
{
System.out.print("* ");
}
System.out.println();
}
}
}
Third: It is a third pattern designed using stars which is also called a hollow pyramid.
class ThirdPattern
{
public static void main(String[] args)
{
int i, j, k, row=5;
for (i=1; i<= row ; i++)
{
for (k = i; k < row ; k++)
{
System.out.print(" ");
}
for (j = 1; j <= (2*i -1); j++)
{
if(j==1 || i == row || j==(2*i-1))
{
System.out.print("*");
}
else
{
System.out.print(" ");
}
}
System.out.println("");
}
}
}
Alphabet Pattern Program in Java
Below two pattern has been designed with the help of alphabets and now to print that pattern the code respective to it has been written in java.
Note: In this code For loop with variable (i) is used for rows, variable (k) is used for column, and variable (j) for spaces.
First: It is the first program designed using the alphabet.
class FourthPattern
{
public static void main(String args[])
{
int i, j,k,l,row = 5;
int alphabet = 65;
for (i= 0; i<= row-1 ; i++)
{
for (j=row-1; j>=i; j--)
{
System.out.print(" ");
}
for (k=0; k<=i; k++)
{
System.out.print((char) (alphabet+k));
}
for(l=i-1; l>=0 ;l--)
{
System.out.print((char) (alphabet+l));
}
System.out.println();
}
}
}
Second: It is a first program designed using the alphabet in which as the number of rows increases the number of alphabets will also increase as form 1 to 3 and vice-versa.
class FourthPattern
{
public static void main(String args[])
{
int i, j,k,l,row = 5,np=1;
int alphabet = 65;
for (i= 0; i<= row ; i++)
{
for (j=row-1; j>i; j--)
{
System.out.print(" ");
}
for (k=0; k<np; k++)
{
System.out.print((char) (alphabet+i));
}
np=np+2;
System.out.println();
}
}
}
Number Pyramid Pattern Program in Java
In this three patterns are designed with the help of numbers and now it has been coded in java to print the respective pattern.
Note: In this code For loop with variable (i) is used for rows, variable (J) is used for the column, and variable (k) for spaces.
First: It is the first pattern designed using numbers.
class SixthPattern
{
public static void main(String args[])
{
int i, j,k, row = 5;
for (i=1; i<=row; i++)
{
for (k=1; k<=row-i; k++)
{
System.out.print(" ");
}
for (j=1; j<=i; j++ )
{
System.out.print(i+" ");
}
System.out.println();
}
}
}
Second: It is a Second pattern designed using numbers.
class SeventhPattern
{
public static void main(String args[])
{
int i, j,k,num=1, row = 5;
for (i=1; i<=row; i++)
{
for (k=1; k<=row-i; k++)
{
System.out.print(" ");
}
for (j=1; j<=i; j++ )
{
System.out.print(num+" ");
num=num+1;
}
System.out.println();
}
}
}
Third: It is a third pattern designed using numbers.
class EightPattern
{
public static void main(String args[])
{
int i, j,k ,row = 5;
for (i= 1; i<= row ; i++)
{
for (k=1; k<=row-i; k++)
{
System.out.print(" ");
}
for (j=1; j<=i; j++)
{
System.out.print(j);
}
for(j=i-1;j>=1;j--)
{
System.out.print(j);
}
System.out.println("");
}
}
}
Discover more from easytechnotes
Subscribe to get the latest posts sent to your email.