5

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();
}
Rand Mate
  • 453
  • 3
  • 8
  • 14

5 Answers5

2

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); 
arshajii
  • 127,459
  • 24
  • 238
  • 287
Serge
  • 6,088
  • 17
  • 27
2

Another solution with a simpler logic:

public static void main(String[] args) {
    int input = 5;

    for (int i = 1; i <= 2 * input - 1; i++) {
        for (int j = 0; j < input - Math.abs((input - i)); j++)
            System.out.print(i);
        System.out.println();
    }
}

You print the elements with respect to the absolute value of their difference from input. If you change the input to another value this will still work.

Juvanis
  • 25,802
  • 5
  • 69
  • 87
  • 1
    the iteration is understandable than the previous one, which model of code is efficient by the way, – Rand Mate Oct 08 '12 at 09:34
  • 1
    Sergei's answer uses 1 loop, but mine uses 2 loops. So for bigger numbers of input (in our case it's 5) his code will run faster, I guess. anyway, you can upvote my answer if it is smart. – Juvanis Oct 08 '12 at 09:57
1

Here's a solution with no loops (recursive)

public class NumberTriangle {        

    public static void print(int top_, int count_, int length_) {
        int top = top_;
        int count = count_;
        int length = length_;
        count++;        

        if (count <= top){
            length++;           
        } else {
            length--;
        }

        if (length == 0) {
            return;
        }       

        String s = String.format(String.format("%%0%dd", length), 0).replace("0",""+count);     
        System.out.println(s);

        NumberTriangle.print(top, count, length);
    }

    public static void main (String args[]){

        NumberTriangle.print(5,0,0);

    }   

}
Tulains Córdova
  • 2,559
  • 2
  • 20
  • 33
0

solution for the problem:

IntStream.range(MAX * -1, MAX)
            .forEach(i -> IntStream.rangeClosed(1, MAX - Math.abs(i))
                    .mapToObj(j -> j == MAX - Math.abs(i) ? MAX - Math.abs(i) + "\n" : MAX - Math.abs(i) + " ")
                    .forEach(System.out::print)
            ); 
Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
-1

@RandMate you could do it this way: to print this pattern you will have to divide the pattern into 2 parts and use two sets of nested for loop: here goes: int i,j,p; /first nested loop to print 1 22 333 4444 55555/ for(i=1;i<=5;i++)
{ for(j=1;j<=i;j++) { System.out.print(i); } System.out.println(); }

/*this is the second nested loop to print
 6666
 777
 88
 9 */
    p=6;
  for(i=4;i>=1;i--)
  {
   for(j=1;j<=i;j++)
   {
    System.out.print(p);
   }
    p=p+1;
   System.out.println();
  }

  Hope it was helpul.
  ALL THE BEST......enjoy!