Java for loop display pattern 2

Question

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

Pattern:/*from   ww w .j  a  va 2  s.co m*/
                            1 
                          2 1 2 
                        3 2 1 2 3 
                      4 3 2 1 2 3 4 
                    5 4 3 2 1 2 3 4 5 
                  6 5 4 3 2 1 2 3 4 5 6 
                7 6 5 4 3 2 1 2 3 4 5 6 7 
              8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 
            9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 
          10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 
        11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 
      12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 
    13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 
  14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 

public class Main {
  public static void main(String[] args) {
    // Create a Scanner object

    int numberOfLines = 15;

    // Display pyramid
    //your code here
  }//from   www  . j a v a2s.c o  m
}




public class Main {
  public static void main(String[] args) {
    // Create a Scanner object

    int numberOfLines = 15;

    // Display pyramid
    for (int rows = 1; rows <= numberOfLines; rows++) {
      // Create spaces in each row
      for (int s = numberOfLines - rows; s >= 1; s--) {
        System.out.print("  ");
      }
      // Create decending numbers in each row
      for (int l = rows; l >= 2; l--) {
        System.out.print(l + " ");
      }
      // Create ascending number in each row
      for (int r = 1; r <= rows; r++) {
        System.out.print(r + " ");
      }
      // End line
      System.out.println();
    }
  }
}



PreviousNext

Related