Search code examples
javabluej

Pattern in java using numbers


I am trying to print the following pattern but don't get the logic how to.. Can you please help me out.. I am using BlueJ and this is my first question so I am not sure what is required.

                1
               2 2
             3 3 3 3
           4 4 4 4 4 4

Thank You in advance.

i have tried this

public class Program92
{
    public static void main()
    {
        for(int x=1;x<=5;x++)
        {
            for(int j=1;j<=x;j++)
             System.out.print(x);
            System.out.println();
        }
    }
}

but could only get

                1
                22
                333
                4444
                55555

Solution

  • you can try this snippet of code as it will print first blank space then your number. and also after every number there is blank space.

    public static void main(String[] args) {
        int l= 5;int k=0;
         for(int x=1;x<5;x++)
            {
             for(int i=l*2-1;i>0;i--)
             {  
                if(x == 1 && i ==1)
                    break;
                System.out.print(" "); 
             }
             System.out.print(x); 
             System.out.print(" "); 
             for(int i=1;i<x*2-2;i++)
             {
                 System.out.print(x); 
                 System.out.print(" "); 
             }
             System.out.println();
             l--;
            }
    
            }