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.
- Square number pattern in Java
- Hollow number square pattern
- left triangle number pattern in Java
- Right triangle number pattern in java
- Hollow number left triangle pattern in java
- Hollow number right triangle pattern in java
- Left down number triangle
- Right down number triangle
- pyramid number pattern in Java
- Hollow pyramid number pattern in Java
- Reverse pyramid number pattern in Java
- Reverse hollow pyramid number pattern in Java
- Diamond number pattern in Java
- Hollow diamond number pattern in Java
- Hourglass number pattern
- Right Pascal number pattern
- Left Pascal number pattern
- Heart number pattern
- Plus number pattern
- X number pattern
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:
- Create a new Java class and take a variable size and assign it with a value.
- Create a nested for loop where the external loop prints rows and the inner loop prints columns.
- In the internal loop print the number using the value of the j variable.
- 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:
- Set the size of the square and create a variable num to store the number counting from 1.
- Execute the external loop number of times as the size of the square.
- 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.
- Reset the num variable to 1.
- 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:
- Take the size of your triangle.
- Create a nested for loop where the external loop repeats the internal loop number of times as the size of the triangle.
- Repeat the inner loop number of times as the row number.
- 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:
- Set some size of your triangle.
- 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.
- 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.
- 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:
- Set the size of the left hollow triangle.
- Create a nested for loop where the external loop repeats the internal loop number of times as the size of the triangle.
- Repeat the inner loop number of times as the row number.
- 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:
- Set the size of the right hollow triangle.
- 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.
- Use the first internal loop to print space a number of times as the size minus row number.
- Use the second internal loop to print numbers only at the boundaries of the triangle.
- 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:
- Take the size of the left-down triangle.
- 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.
- 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:
- Take the size of the right-down triangle.
- 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.
- Use first internal loop to print spaces then use second internal loop to print numbers in reverse order.
- 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:
- Take the size of the pyramid.
- We will have nested loops where the first internal loop will handle space and the second internal loop will handle numbers.
- Use the first internal loop and print spaces for the times as the size minus row number.
- Use the second internal loop to print numbers in order 1, 123, 12345, 1234567, and so on.
- 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
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:
- Take the size of the hollow pyramid.
- The first internal loop prints spaces for the times as the size minus row number.
- Use the second internal loop to print numbers for the time as the row number multiplied by 2 plus 1.
- 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.