Write a program in java using for loop:-
12345
1234
123
12
1
Answers
Answered by
1
Your program:
package com.company;
class Main {
public static void main (String[] args) {
for( int i=5; i>0;i--)
{
for( int j=1; j<=i;j++)
{
System.out.print(j);
}
System.out.println();
}
}
}
Answered by
1
Required Answer:-
Question:
Write a Java program to display the following pattern.
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Solution:
Here comes the códe.
public class Pattern {
public static void main(String[] args) {
int i, j;
for(i=5;i>=1;i--) {
for(j=1;j<=i;j++)
System.out.print(j+" ");
System.out.println();
}
}
}
Dry Run:
- When i = 5, inner loop displays numbers from 1 to 5.
- When i = 4, inner loop displays numbers from 1 to 4
- When i = 3, inner loop displays numbers from 1 to 3 and so on. In this way, the program works.
See the attachment for output ☑.
Attachments:


Similar questions