Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
}
like image 887
Rand Mate Avatar asked Oct 06 '22 10:10

Rand Mate


1 Answers

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); 
like image 69
Serge Avatar answered Oct 10 '22 02:10

Serge