Computer Science, asked by arijit1587, 1 year ago

write a java program to display the following number pyramid

Attachments:

Answers

Answered by QGP
10

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 i^{th} row, the starting number is i itself. Also, there are i elements in each row.

So we must run an inner loop to run i times. Also, we would need another variable to start from i 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

       }

   }

}



Attachments:

arijit1587: verrrrrrrrrrry much thanks to u
QGP: You are welcome :)
Answered by onlinestudy
1

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();

}

}

}


Similar questions