Write a program in java using for loop.
('0' in the below question was not meant to be used. Please ignore all the zeros)
12345
01234
00123
00012
00001
Answers
Answered by
0
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( );
}
}
HOPE YOU WILL UNDERSTAND AND MARK AS BRAIN LIST
Answered by
1
Required Answer:-
Correct Question:
Write a Java program to display the following pattern.
12345
1234
123
12
1
Solution:
Here comes the program.
public class Pattern {
public static void main(String[] args) {
int i, j, space=0;
for(i=5;i>=1;i--,space++) {
for(j=1;j<=space;j++)
System.out.print(" ");
for(j=1;j<=i;j++)
System.out.print(j);
System.out.println();
}
}
}
Explanation:
- Outer loop determines the number of rows for the pattern.
- First loop inside the outer loop displays the spaces and the second loop inside the outer loop displays the number.
- After displaying each row, a new line is displayed.
See the attachment for output ☑.
Attachments:
Similar questions
Social Sciences,
1 month ago
English,
1 month ago
Math,
3 months ago
Math,
3 months ago
Business Studies,
9 months ago