Java for loop display pattern 6

Question

We would like to use for loop the show the following pattern.


1 2 3 4 5 6 
  1 2 3 4 5 
    1 2 3 4 
      1 2 3 
        1 2 
          1 
public class Main {
  public static void main(String[] args) {
    String padding = "  ";
    System.out.println("Pattern");
    //your code
  }
}



public class Main {
  public static void main(String[] args) {
    String padding = "  ";
    System.out.println("Pattern");
    for (int i = 6; i >= 1; i--) {
      // print padding
      for (int j = i; j < 6; j++) {
        System.out.print(padding);
      }
      // print left half-pyramid (upside down)
      for (int k = 1; k <= i; k++) {
        System.out.printf("%-2d", k);
      }
      System.out.println();
    }
  }
}



PreviousNext

Related