Write java code to display the following:
4 3 2 1
3 2 1
2 1
1
Answers
Answered by
3
public class Pattern
{
public static void main(String[] args)
{
for(int i=1;i<=4;i++) //i does the work of printing 4 rows
{
for(int j=1;j<i;j++) //j prints required spaces before numbers in each row
{
System.out.print(" ");
}
for(int k=5-i;k>0;k--) //k prints the numbers in descending order in each row
{
System.out.print(k+" ");
}
System.out.println();
}
}
}
{
public static void main(String[] args)
{
for(int i=1;i<=4;i++) //i does the work of printing 4 rows
{
for(int j=1;j<i;j++) //j prints required spaces before numbers in each row
{
System.out.print(" ");
}
for(int k=5-i;k>0;k--) //k prints the numbers in descending order in each row
{
System.out.print(k+" ");
}
System.out.println();
}
}
}
Similar questions