write a java program to display the following number pyramid
Answers
Pattern in Java
Java pattern programs usually require some analysis on our part. First of all, we see that there are 5 rows. So we must run a loop to run 5 times.
In the row, the starting number is itself. Also, there are elements in each row.
So we must run an inner loop to run times. Also, we would need another variable to start from and increment until the elements in row are printed.
Here is a Java Program:
public class Pattern
{
public static void main(String[] args)
{
for(int i=1;i<=5;i++) //Loop to print 5 rows
{
int k=i; //Starting Point in each row
for(int j=0;j<i;j++) //Loop runs i times to print i elements in a row
{
System.out.print(k+" "); //Printing k
k++; //Incrementing k
}
System.out.println(); //Printing a blank line after row is over
}
}
}
class piramid{
public static void main(String[]args){
System.out.println("*******************************************");
for(int i = 1; i < 10; i++){
for(int j = 1; j<i; j++){
System.out.print(j);
}
System.out.println();
}
}
}