Number Patterns using loops in Java

前端 未结 7 2098
感动是毒
感动是毒 2021-01-25 04:30

I have been trying different variations of for loops and have no clue how to make these patterns:

Pattern 1

54321
5432
543
54
5

Pattern

7条回答
  •  失恋的感觉
    2021-01-25 05:05

    Since you posted an attempt for pattern one, I'll tell you a solution for pattern one -

    int rows = 5; // <- Start at 5.
    for (int i = rows; i > 0; i--) { // <- Use decrementing loop(s).
      for (int j = rows; j > rows - i; j--) { // <- Start at 5 (again)
        System.out.print(j);
      }
      System.out.println();
    }
    

    Output is pattern 1 in your question,

    54321
    5432
    543
    54
    5
    

提交回复
热议问题