0

i wanted to display a pattern like this
4444
333
22
1
i used the following code
import java.util.*;

public class PatternExp
{
public static void main (String args[])
{
Scanner src=new Scanner(System.in);
System.out.println("Enter the number of lines needed");
int n=src.nextInt();
for (int i=n; i>0; i--)
{ for(int j=n; j>0; j--)

            {System.out.print(i+" ");}

              System.out.println();}
}

}

but got an output like
4444
3333
2222
1111

what alterations should be done in the code???
Please reply

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by pritish.kamath
0

Here is the correct code: (I have created a simple class called A just to show how it works; use n in place of 5)

class A
{
    public static void main (String[] args) 
    {
        for (int i=5; i>0; i--)
        { 
     for(int j=i; j>0; j--)
     {
         System.out.print(i+" ");
     }
              System.out.println();
        }

    }
}

Always indent your code it makes it looker easier to understand and also looks good.

Hope this helps :-)

0

class A
{
public static void main (String[] args)
{
for (int i=5; i>0; i--)
{
for(int j=i; j>0; j--)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}

That worked just fine
Thanx
I will try to improve the indentation problem next time

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.