20 Number Pattern In Java


In this article, we will create 20 different number pattern in Java with complete code and a description of each pattern.

A number pattern program is a series of numbers that generate a certain pattern or geometrical shape like a square or triangle.

These patterns enhance your programming skills and are generated with the help of loops and if-else statements.


  1. Square number pattern in Java
  2. Hollow number square pattern
  3. left triangle number pattern in Java
  4. Right triangle number pattern in java
  5. Hollow number left triangle pattern in java
  6. Hollow number right triangle pattern in java
  7. Left down number triangle
  8. Right down number triangle
  9. pyramid number pattern in Java
  10. Hollow pyramid number pattern in Java
  11. Reverse pyramid number pattern in Java
  12. Reverse hollow pyramid number pattern in Java
  13. Diamond number pattern in Java
  14. Hollow diamond number pattern in Java
  15. Hourglass number pattern
  16. Right Pascal number pattern
  17. Left Pascal number pattern
  18. Heart number pattern
  19. Plus number pattern
  20. X number pattern
number pattern in Java

1. Square number pattern in Java

The square number pattern is a number pattern that is generated by printing the numbers in a square shape.

12345
12345
12345
12345
12345

The square number pattern is the easiest pattern to create. Here are the steps you can proceed with:

  1. Create a new Java class and take a variable size and assign it with a value.
  2. Create a nested for loop where the external loop prints rows and the inner loop prints columns.
  3. In the internal loop print the number using the value of the j variable.
  4. Break the line using the System.out.println() method.
public class square {
  public static void main(String args[]) {
    // size of square
    int size = 5;

    // loop to print square
    for (int i = 0; i < size; i++) {
      for (int j = 1; j <= size; j++) {
        System.out.print(j);
      }
      System.out.println();
    }
  }
}

Output:

12345
12345
12345
12345
12345

2. Hollow number square pattern

The hollow square number pattern is similar to the square pattern but hollow inside.

12345
1   2
1   2
1   2
12345

Here are the steps you can proceed with to create a hollow square number pattern:

  1. Set the size of the square and create a variable num to store the number counting from 1.
  2. Execute the external loop number of times as the size of the square.
  3. In the inner loop, check if the row number is 0 or size - 1. If yes, print the number using the num++ variable. If not, then check if the column number is 0 or size - 1. If yes, print the number using the num++ variable. If not, then print a space.
  4. Reset the num variable to 1.
  5. Break the line.
public class hollowSquare {
  public static void main(String[] args) {

    // size of the square
    int size = 5;
    int num = 1;
    // outer loop
    for (int i = 0; i < size; i++) {
      // inner loop
      for (int j = 0; j < size; j++) {
        // print only numbers in first and last row
        if (i == 0 || i == size - 1) {
          System.out.print(num++);
        } else {
          // print numbers only at first and last position row
          if (j == 0 || j == size - 1) {
            System.out.print(num++);
          } else {
            System.out.print(" ");
          }
        }
      }
      num = 1;
      System.out.println();
    }
  }
}

Output:

12345
1   2
1   2
1   2
12345

3. Left triangle number pattern in Java

The left triangle number pattern is a number pattern that has a shape of a triangle with perpendicular on the left side.

1
12
123
1234
12345

Steps to create a left triangle number pattern in Java:

  1. Take the size of your triangle.
  2. Create a nested for loop where the external loop repeats the internal loop number of times as the size of the triangle.
  3. Repeat the inner loop number of times as the row number.
  4. Break the line at the end of the inner loop.
public class leftTrianlge {
  public static void main(String[] args) {

    // size of the triangle
    int size = 5;
    // loop to print the pattern
    for (int i = 0; i < size; i++) {
      // print column
      for (int j = 0; j <= i; j++) {
        System.out.print(j+1);
      }
      System.out.println();
    }
  }
}

Output:

1
12
123
1234
12345

4. Right triangle number pattern in Java

The right triangle number pattern is a number pattern that has a shape of a triangle with perpendicular to the right side.

    1
   12
  123
 1234
12345

Steps to create a right triangle number pattern in Java:

  1. Set some size of your triangle.
  2. Create a nested for loop. Here we will have 2 internal loops. The first internal loop will print spaces and the second internal loop will print numbers.
  3. The first internal loop will print spaces for the times as the size minus row number. The second internal loop will print numbers for the times as the row number.
  4. Break the line at the end of the second inner loop.
public class rightTrianlge {
  public static void main(String[] args) {

    // size of the triangle
    int size = 5;
    int num = 1;
    // loop to print the pattern
    for (int i = 0; i < size; i++) {
      // print spaces
      for (int j = 1; j < size - i; j++) {
        System.out.print(" ");
      }
      // print numbers
      for (int k = 0; k <= i; k++) {
        System.out.print(num++);
      }
      num = 1;
      System.out.println();
    }
  }
}

Output:

    1
   12
  123
 1234
12345

5. Hollow Number Left triangle In Java

The hollow left triangle number pattern is a number pattern that has a shape of a triangle, perpendicular on the left side and is hollow inside.

1
12
1 2
1  2
12345

Steps to create a hollow left triangle number pattern in Java:

  1. Set the size of the left hollow triangle.
  2. Create a nested for loop where the external loop repeats the internal loop number of times as the size of the triangle.
  3. Repeat the inner loop number of times as the row number.
  4. Break the line at the end of the inner loop.
public class hollowLeftTrianlge {
  public static void main(String[] args) {

    // size of the triangle
    int size = 5;
    int num = 1;
    for (int i = 1; i <= size; i++) {
      for (int j = 0; j < i; j++) {
        // not last row
        if (i != size) {
          if (j == 0 || j == i - 1) {
            System.out.print(num++);
          } else {
            System.out.print(" ");
          }
        }
        // last row
        else {
          System.out.print(num++);
        }
      }
      num = 1;
      System.out.println();
    }
  }
}

Output:

1
12
1 2
1  2
12345

6. Hollow Number Right triangle In Java

The hollow right triangle number pattern is a number pattern that has a shape of a triangle, perpendicular on the right side and is hollow inside.

    1
   12
  1 2
 1  2
12345

Steps to create a hollow right triangle number pattern in Java:

  1. Set the size of the right hollow triangle.
  2. Create a nested for loop. We will have to print both spaces and numbers so create 2 internal loops. The first internal loop will print spaces and the second internal loop will print numbers.
  3. Use the first internal loop to print space a number of times as the size minus row number.
  4. Use the second internal loop to print numbers only at the boundaries of the triangle.
  5. Break the line at the end of the second inner loop and reset the num to 1.
public class hollowRightTrianlge {
  public static void main(String[] args) {

    // size of the triangle
    int size = 5;
    int num = 1;
    for (int i = 1; i <= size; i++) {
      // print space
      for (int j = 1; j <= size - i; j++) {
        System.out.print(" ");
      }
      // print numbers
      for (int j = 0; j < i; j++) {
        // not last row
        if (i != size) {
          if (j == 0 || j == i - 1) {
            System.out.print(num++);
          } else {
            System.out.print(" ");
          }
        }
        // last row
        else {
          System.out.print(num++);
        }
      }
      num = 1;
      System.out.println();
    }
  }
}

Output:

    1
   12
  1 2
 1  2
12345

7. Left Down Number Triangle

The left down number triangle is the water image of the left number triangle.

12345
1234
123
12
1

Steps to create a left down number triangle in Java:

  1. Take the size of the left-down triangle.
  2. Create a nested for loop. Use an internal loop to print numbers for the times as the row number and reduce the size by 1 for each row.
  3. Break the line at the end of internal loop and set num to 1.
public class leftDown {
  public static void main(String[] args) {
    // size of the triangle
    int size = 5;
    int num = 1;
    
    for (int i = 0; i < size; i++) {
      // print numbers
      for (int j = 0; j < size - i; j++) {
        System.out.print(num++);
      }
      num = 1;
      System.out.println();
    }
  }
}

Output:

12345
1234
123
12
1

8. Right Down Number Triangle

The right down number triangle is the water image of the right number triangle.

12345
 1234
  123
   12
    1

Steps to create a right down number triangle in Java:

  1. Take the size of the right-down triangle.
  2. Create a nested for loop. It will have 2 internal loops where the first internal loop will print spaces and the second loop will print numbers.
  3. Use first internal loop to print spaces then use second internal loop to print numbers in reverse order.
  4. Break the line at the end of internal loop and set num to 1.
public class rightDown {
  public static void main(String[] args) {
    // size of the triangle
    int size = 5;
    int num = 1;

    for (int i = 0; i < size; i++) {
      // print spaces
      for (int j = 0; j < i; j++) {
        System.out.print(" ");
      }
      // print numbers
      for (int j = 0; j < size - i; j++) {
        System.out.print(num++);
      }
      num = 1;
      System.out.println();
    }
  }
}

Output:

12345
 1234
  123
   12
    1

9. Pyramid Number Pattern In Java

The pyramid number pattern is a number pattern that has a shape of a pyramid. It is a very famous pattern program in Java.

    1
   123
  12345
 1234567
123456789

Steps to create a pyramid number pattern in Java:

  1. Take the size of the pyramid.
  2. We will have nested loops where the first internal loop will handle space and the second internal loop will handle numbers.
  3. Use the first internal loop and print spaces for the times as the size minus row number.
  4. Use the second internal loop to print numbers in order 1, 123, 12345, 1234567, and so on.
  5. Break the line at the end of the loop.
public class pyramid {
  // pyramid number pattern
  public static void main(String[] args) {

    int size = 5;
    int num = 1;
    for (int i = 0; i < size; i++) {
      // print spaces
      for (int j = 0; j < size-i-1; j++) {
        System.out.print(" ");
      }
      // print numbers
      for (int k = 0; k < 2*i+1; k++) {
        System.out.print(num++);
      }
      num = 1;
      System.out.println();
    }
  }
}

Output:

    1
   123
  12345
 1234567
123456789

Stay Ahead, Learn More


10. Hollow Pyramid Number Pattern In Java

The hollow pyramid number pattern is a number pattern that has a shape of a hollow pyramid.

    1
   1 2
  1   2
 1     2
123456789

Steps to create a hollow pyramid number pattern in Java:

  1. Take the size of the hollow pyramid.
  2. The first internal loop prints spaces for the times as the size minus row number.
  3. Use the second internal loop to print numbers for the time as the row number multiplied by 2 plus 1.
  4. If it's first or last row print only numbers if not print numbers only at the first and last position or the row.
public class hollowPyramid {
  public static void main(String[] args) {
    // size of the pyramid
    int size = 5;
    int num = 1;
    for (int i = 0; i < size; i++) {
      // print spaces
      for (int j = 0; j < size-i-1; j++) {
        System.out.print(" ");
      }
      // print numbers
      for (int k = 0; k < 2*i+1; k++) {
        if (i == 0 || i == size - 1) {
          System.out.print(num++);
        } else {
          if (k == 0 || k == 2*i) {
            System.out.print(num++);
          } else {
            System.out.print(" ");
          }
        }
      }
      num = 1;
      System.out.println();
    }
  }
}

Output:

    1
   1 2
  1   2
 1     2
123456789

11. Reverse Pyramid Number Pattern In Java

The reverse pyramid number pattern is nothing but pyramid reversed.

123456789
 1234567
  12345
   123
    1

Here is the complete code to create a reverse pyramid number pattern in Java:

public class reversePyramid {
  public static void main(String[] args) {
    // size of the pyramid
    int size = 5;
    int num = 1;
    for (int i = 0; i < size; i++) {
      // print spaces
      for (int j = 0; j < i; j++) {
        System.out.print(" ");
      }
      // print numbers
      for (int k = 0; k < 2*(size-i)-1; k++) {
        System.out.print(num++);
      }
      num = 1;
      System.out.println();
    }
  }
}

Output:

123456789
 1234567
  12345
   123
    1

12. Reverse hollow pyramid number pattern in Java

The reverse hollow pyramid number pattern is nothing but a hollow pyramid reversed.

123456789
 1     2
  1   2
   1 2
    1

Here is the complete code to create a reverse hollow pyramid number pattern in Java:

public class reverseHollowPyramid {
  public static void main(String[] args) {
    // size of the pyramid
    int size = 5;
    int num = 1;
    for (int i = 0; i < size; i++) {
      // print spaces
      for (int j = 0; j < i; j++) {
        System.out.print(" ");
      }
      // print numbers
      for (int k = 0; k < 2*(size-i)-1; k++) {
        if (i == 0 || i == size - 1) {
          System.out.print(num++);
        } else {
          if (k == 0 || k == 2*(size-i-1)) {
            System.out.print(num++);
          } else {
            System.out.print(" ");
          }
        }
      }
      num = 1;
      System.out.println();
    }
  }
}

Output:

123456789
 1     2
  1   2
   1 2
    1

13. Diamond Number Pattern In Java

The diamond number pattern in Java can be created by using and modifying the code of pyramid and reverse pyramid pattern.

    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1

Here is the complete code to create a diamond number pattern in Java:

public class diamond {
  public static void main(String[] args) {
    int size = 5;
    int num = 1;
    // upside pyramid
    for (int i = 1; i <= size; i++) {
      // printing spaces
      for (int j = size; j > i; j--) {
        System.out.print(" ");
      }
      // printing numbers
      for (int k = 0; k < i * 2 - 1; k++) {
        System.out.print(num++);
      }
      num = 1;
      System.out.println();
    }
    // downside pyramid
    for (int i = 1; i <= size - 1; i++) {
      // printing spaces
      for (int j = 0; j < i; j++) {
        System.out.print(" ");
      }
      // printing numbers
      for (int k = (size - i) * 2 - 1; k > 0; k--) {
        System.out.print(num++);
      }
      num = 1;
      System.out.println();
    }
  }
}

Output:

    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1

14. Diamond Hollow Number Pattern In Java

The diamond hollow number pattern in Java can be created by using and modifying the code of hollow pyramid and reverse hollow pyramid pattern.

    1
   1 2
  1   2
 1     2
1       2
 1     2
  1   2
   1 2
    1

Here is the complete code to create a diamond hollow number pattern in Java:

public class hollowDiamond {
  public static void main(String[] args) {
    int size = 5;
    int num = 1;
    // upside pyramid
    for (int i = 1; i <= size; i++) {
      // printing spaces
      for (int j = size; j > i; j--) {
        System.out.print(" ");
      }
      // printing numbers
      for (int k = 0; k < i*2-1; k++) {
        if (k == 0 || k == 2*i-2) {
          System.out.print(num++);
        } else {
          System.out.print(" ");
        }
      }
      num = 1;
      System.out.println();
    }
    // downside triangle
    for (int i = 1; i < size; i++) {
      // printing spaces
      for (int j = 0; j < i; j++) {
        System.out.print(" ");
      }
      // printing numbers
      for (int k = (size-i)*2-1; k >= 1; k--) {
        if (k == 1 || k == (size-i)*2-1) {
          System.out.print(num++);
        } else {
          System.out.print(" ");
        }
      }
      num = 1;
      System.out.println();
    }
  }
}

Output:

    1
   1 2
  1   2
 1     2
1       2
 1     2
  1   2
   1 2
    1

15. Hourglass Number Pattern In Java

The hourglass number pattern in Java has the shape of an hourglass and is made up of a mixture of the reverse pyramid and pyramid patterns.

123456789
 1234567
  12345
   123
    1
   123
  12345
 1234567
123456789

Here is the complete code to create a hourglass number pattern in Java:

public class hourGlass {
  public static void main(String[] args) {
    int size = 5;
    int num = 1;
    // reversed pyramid number pattern
    for (int i = 0; i < size; i++) {
      // printing spaces
      for (int j = 0; j < i; j++) {
        System.out.print(" ");
      }
      // printing numbers
      for (int k = 0; k < (size - i) * 2 - 1; k++) {
        System.out.print(num++);
      }
      num = 1;
      System.out.println();
    }
    // pyramid number pattern
    for (int i = 2; i <= size; i++) {
      // printing spaces
      for (int j = size; j > i; j--) {
        System.out.print(" ");
      }
      // printing number
      for (int k = 0; k < i * 2 - 1; k++) {
        System.out.print(num++);
      }
      num = 1;
      System.out.println();
    }
  }
}

Output:

123456789
 1234567
  12345
   123
    1
   123
  12345
 1234567
123456789

16. Right Pascal Triangle Number Pattern

Here is how right pascal triangle number pattern looks like.

1
12
123
1234
12345
1234
123
12
1

Here is the complete code to create a right pascal triangle number pattern in Java:

public class leftPascal {
  public static void main(String[] args) {
    // left pasal triangle
    int size = 5;
    int num = 1;

    for (int i = 1; i <= size; i++) {
      for (int j = 0; j < i; j++) {
        System.out.print(num++);
      }
      num = 1;
      System.out.println();
    }
    
    for (int i = 1; i <= size - 1; i++) {
      for (int j = 0; j < size - i; j++) {
        System.out.print(num++);
      }
      num = 1;
      System.out.println();
    }
  }
}

Output:

1
12
123
1234
12345
1234
123
12
1

17. Left Pascal Triangle Number Pattern

Here is how left pascal triangle number pattern looks like.

    1
   12
  123
 1234
12345
 1234
  123
   12
    1

Here is the complete code to create a left pascal triangle number pattern in Java:

public class leftPascal {
  public static void main(String[] args) {
    // left pasal triangle
    int size = 5;
    int num = 1;

    for (int i = 1; i <= size; i++) {
      for (int j = 0; j < size - i; j++) {
        System.out.print(" ");
      }
      for (int k = 0; k < i; k++) {
        System.out.print(num++);
      }
      num = 1;
      System.out.println();
    }
    for (int i = 1; i <= size - 1; i++) {
      for (int j = 0; j < i; j++) {
        System.out.print(" ");
      }
      for (int k = 0; k < size - i; k++) {
        System.out.print(num++);
      }
      num = 1;
      System.out.println();
    }
  }
}

Output:

    1
   12
  123
 1234
12345
 1234
  123
   12
    1

18. Heart Number Pattern In Java

The heart number pattern is a very complex pattern. It has the shape of a heart.

 12   34
1234 5678
123456789
 1234567
  12345
   123
    1

Here is the complete code to create a heart number pattern in Java:

public class heart {
  public static void main(String[] args) {
    // heart number pattern
    int size = 5;
    int num = 1;

    for (int i = size / 2; i < size; i += 2) {
      // print first spaces
      for (int j = 1; j < size - i; j += 2) {
        System.out.print(" ");
      }
      // print first numbers
      for (int j = 1; j < i + 1; j++) {
        System.out.print(num++);
      }
      // print second spaces
      for (int j = 1; j < size - i + 1; j++) {
        System.out.print(" ");
      }
      // print second numbers
      for (int j = 1; j < i + 1; j++) {
        System.out.print(num++);
      }
      num = 1;
      System.out.println();
    }
    // lower part
    // inverted pyramid
    for (int i = size; i > 0; i--) {
      for (int j = 0; j < size - i; j++) {
        System.out.print(" ");
      }
      for (int j = 1; j < i * 2; j++) {
        System.out.print(num++);
      }
      num = 1;
      System.out.println();
    }
  }
}

Output:

 12   34
1234 5678
123456789
 1234567
  12345
   123
    1

19. Plus Number Pattern In Java

The plus number pattern is a pattern made up of numbers having a plus shape.

  1  
  2  
12345
  4  
  5

Here is the complete code to create a plus number pattern in Java:

public class plus {
  public static void main(String[] args) {
    // size of plus, use odd number
    int size = 5;
    int numH = 1; // number horizontal
    int numV = 1; // number vertical

    for (int i = 0; i < size; i++) {
      for (int j = 0; j < size; j++) {
        // print only stars in middle row
        if (i == size / 2) {
          System.out.print(numH++);
        }
        // other than middle row, print numbers only at index size/2
        else {
          if (j == size / 2) {
            System.out.print(numV++);
          } else {
            System.out.print(" ");
          }
        }
      }
      if (i == size / 2) {
        numV++;
      }
      System.out.println();
    }
  }
}

Output:

  1
  2
12345
  4
  5

20. X Number Pattern In Java

The X number pattern is a pattern made up of numbers having a cross shape.

1   2
 1 2 
  1  
 1 2 
1   2

Here is the complete code to create a cross number pattern in Java:

public class cross {
  public static void main(String[] args) {
    // size of cross, use odd number
    int size = 5;
    int num = 1;

    for (int i = 0; i < size; i++) {
      for (int j = 0; j < size; j++) {
        if (i == j || i + j == size - 1) {
          System.out.print(num++);
        } else {
          System.out.print(" ");
        }
      }
      num = 1;
      System.out.println();
    }
  }
}

Output:

1   2
 1 2
  1
 1 2
1   2

Conclusion

In this article, you learned and created 20 different number pattern in Java. You can checkout number pattern in Javascript and number pattern in Python.