Java for loop display pattern 5

Question

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

Pattern
          1 
        2 1 
      3 2 1 
    4 3 2 1 
  5 4 3 2 1 
6 5 4 3 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 = 1; i <= 6; i++) {
      // print padding
      for (int j = i; j < 6; j++) {
        System.out.print(padding);
      }
      // print left half-pyramid
      for (int k = i; k >= 1; k--) {
        System.out.printf("%-2d", k);
      }
      System.out.println();
    }
  }
}



PreviousNext

Related