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

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 :-)

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.