Creating number pattern with minimum for loops

How to create the following number pattern with minimum for loops. Is there any names given mathematically for the pattern of numbers as like Fibonacci, pascal’s triangle, any-other interesting patterns which are complicated but possible using for-loops ?

Expected O/P Pattern:

    1
    22
    333
    4444
    55555
    6666
    777
    88
    9 

// For loops only prints only from 1 to 5 it prints correctly and on reversing there comes the wrong output.

for(int i=1; i<10; i++)
{
for(int j=1,k=10; j<=i&&k>5; j++,k--)   
        {
            if(i<=5)
            System.out.print(i);
            else
            if(i>5)
            System.out.print(i);
        }
            System.out.println();
}

Answer

here you are:

for (int i = 1, j = 1 ; i < 10 ; i++, j = (i <= 5) ? (j*10 + 1) : (j/10))
    System.out.println(i * j); 

Leave a Reply

Your email address will not be published. Required fields are marked *