-4

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
Ascalonian
  • 14,409
  • 18
  • 71
  • 103
Anirudh
  • 9
  • 1
  • 1
  • 7
  • 1
    StackOverflow is not a site for solving your homework INSTEAD of you. Please read http://stackoverflow.com/help/how-to-ask – Honza Zidek Feb 24 '15 at 11:54
  • I have tried in my notebook and absolutely can't get it.... – Anirudh Feb 24 '15 at 11:54
  • I'm voting to close this question as off-topic because op's lack of effort to solve this. – Ruchira Gayan Ranaweera Feb 24 '15 at 11:56
  • @RuchiraGayanRanaweera I'm sorry that i troubled you. – Anirudh Feb 24 '15 at 11:59
  • @Anirudh no need to say sorry. Just show your effort here... effort always appreciate here. Also when posting question without an effort will leads close your question without getting any help. – Ruchira Gayan Ranaweera Feb 24 '15 at 12:02
  • @RuchiraGayanRanaweera i added my attempt to the post – Anirudh Feb 24 '15 at 12:11
  • I have a tip for you: The first line is special and doesn't follow the same rule as the other lines. So, print the first line and then use loops for the following lines. – Klas Lindbäck Feb 24 '15 at 12:22
  • @KlasLindbäck i know that it should print the line_number+1 numbers if you keep 1 out.... – Anirudh Feb 24 '15 at 12:29
  • thank you all for the response, I got the pattern after straining my head for an hour.... Do you think it would be fine if I am printing 1 outside the loop? – Anirudh Feb 24 '15 at 13:43
  • hi all can you please look at my question on the same logic but now i need formatting help.. thanks http://stackoverflow.com/questions/28697251/formatting-concerning-patterns/28697623? – Anirudh Feb 24 '15 at 14:04

3 Answers3

0

Since this seems to be homework, see if you can manage to get it to build a right angled triangle with those numbers, so 1 on top, 2 2's beneath it, 4 3's beneath that, etc:

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

Once that you manage that, all that you would need to do would be to figure out how many white space values you need to add before each number.

npinti
  • 51,780
  • 5
  • 72
  • 96
0

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--;
        }

        }
Prashant
  • 2,556
  • 2
  • 20
  • 26
0

Try this:

public class program98
{
public static void main()
{
System.out.print("     "+"1");//5 spaces int the blank
for(int i=1;i<=4;i++)
{
 for(int s=6;s>1;s--)
   {System.out.print(" ");//1 space
    }
  for(int j=1;j<1;j++)
    {System,out.print(i);
     }
    for(int j=1;j<1;j++)
      {System.out.print(i);//prints this twice. Hence,instead of once,the number of times it prints is double
       }
         System.out.println(" ");//1 space
        }
       }
      }