简体   繁体   中英

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

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

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.

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

    }   

}

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

@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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM