Computer Science, asked by vishayadav12345, 1 year ago

Write a program in java to print the pattern... Plz help

Attachments:

Answers

Answered by QGP
0

Pattern in Java

Pattern questions in Java require some analysis on our part.

We first notice that there are 5 rows. So we must run a loop which iterates 5 times.

Next, in each row, the number of elements keeps increasing, while the sequence itself is decreasing.


So we must run an inner loop whose number of iterations is increasing whereas the sequence itself is decreasing. For this, the inner loop variable must depend on the outer loop variable.


Here is a Java program to print the required pattern:


public class Pattern        //Creating Class

{

   public static void main(String[] args)      //The main function

   {

       for(int i=1;i<=5;i++)       //Outer Loop runs 5 times and prints rows

       {

           for(int j=i;j>0;j--)    //Inner Loop runs a decreasing sequence from i to 1

           {

               System.out.print(j+" ");        //Printing j

           }

           System.out.println();       //Printing a new line

       }

   }

}


Attachments:
Similar questions