Computer Science, asked by upadhyayaprerona, 2 months ago

Write a java program toh print this pattern using Nested for-loop.​

Attachments:

Answers

Answered by anindyaadhikari13
3

Answer:

The given co‎de is written in Java.

public class Pattern    {

   public static void main(String args[])  {

       int i,j,n=5;

       for(i=1;i<=n;i++)   {

           for(j=n;j>=i;j--)

               System.out.print(j);

           System.out.println();

       }

   }

}

Variable Description:

\boxed{\begin{array}{c|c|c}\tt \underline{Variable}:&amp;\tt \underline{Type}:&amp;\tt \underline{Function}:\\ \tt i&amp;\tt int&amp;\tt {L}oop\ variable.\\ \tt j&amp;\tt int&amp;\tt {L}oop\ Variable.\\ \tt n&amp;\tt int&amp;\tt Stores\ number\ of\ rows.\end{array}}

See the attachment for output.

•••♪

Attachments:
Answered by kamalrajatjoshi94
2

Answer:

Program:-

public class Pattern

{

public static void main(String args[ ])

{

int i,j;

for(i=1;i<=5;i++)

{

for(j=5;j>=i;j--)

{

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

}

System.out.println();

}

}

}

Thanks this made me more perfect.

How it executed:-

  • The initial value of i is 1 condition is checked is 1<=5 condition is true ,the body enters the inner loop
  • j=5 .The second loop then checks the condition. Is 5>=1.It is true then decremented until j is 1.So it prints 5 4 3 2 1.
  • i=2 condition is true so prints 5 4 3 2
  • i=3 , it prints 5 4 3
  • i=4 ,it prints 5 4
  • i=4, it prints 5

Hope you understood the logic.

Attachments:
Similar questions