Write a java program toh print this pattern using Nested for-loop.
Attachments:
Answers
Answered by
3
Answer:
The given code 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:
See the attachment for output.
•••♪
Attachments:
Answered by
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