Disclosure: When you purchase a service or a product through our links, we sometimes earn a commission.

# Display number patterns using java

January 13, 2018

Here is a list of number patterns that you can produce using any programming language. I use Java in this post. I hope you will find these simple programs useful for programming challenges or assignments you might encounter. You can check also other pattern by clicking here.

Here is the first pattern including the full code. I didn’t include the Scanner class for user input because I wanted to limit the number of code lines to the minimum.
As you can see in the code, the ‘j’ in the inner loop is printed from 1 up to the upper limit set by the value of the ‘number’. The value of the ‘number’ increase by one everytime the outer loop iterates.

1
12
123
1234
12345
123456
1234567
12345678
123456789

public class Pattern112123 {
public static void main( String[] args) {
int number = 1;
for(int i=1; i<=9; i++){

for (int j = 1; j<= number; j++){
System.out.print(j);
}
number += 1;

System.out.println(); //New line
}
}
}

9
98
987
9876
98765
987654
9876543
98765432
987654321

The next one is similar to the previous, but the inner loop is in a decreasing mode.

The code is listed below

public class Pattern998987{
public static void main( String[] args) {
int number = 9;
for(int i=1; i<=9; i++){
for (int j = 9; j>= number; j--){
System.out.print(j);
}
number -= 1;
System.out.println();
}
}
}

1
22
333
4444
55555
666666
7777777
88888888
999999999

We can have a number pattern similar to the above, by tweaking the inner loop so that it prints the ‘number’ repeatedly based on its value as the upper limit. The ‘number’ increases by one in the outer loop. The code is shown below.

public class Pattern1122 {
public static void main( String[] args) {
int number = 1;
for(int i=1; i<=9; i++){
for (int j = 1; j<= number; j++){
System.out.print(number);
}
number += 1;
System.out.println();
}

}
}

9
88
777
6666
55555
444444
3333333
22222222
111111111

Again this one is similar to the above except the direction of the inner loop.

public class Pattern1122 {
public static void main( String[] args) {
int number = 9;
for(int i=1; i<=9; i++){ for (int j = 9; j>= number; j--){
System.out.print(number);
}
number -= 1;
System.out.println();
}

}
}

This one is different from the previous triangles because it starts with one or more spaces which we need to take into account.

We can show the row number and how many spaces or digits are printed in each row using a table. Please note that we can use asterisks or any other character instead of digits.

 Row Spaces number of digits 1 8 1 2 7 2 3 6 3 4 5 4 5 4 5 6 3 6 7 2 7 8 1 8 9 0 9

As we can see, each number of the leading space or digits changes depending on the row number. Our job is to find out the rule that determines the relationship between each of the row number and the spaces and the digits to be printed.

If we start from the spaces, we can see that when the row number increases by one the spaces are decreased by one. We can make a rule for that by using a constant number. In this case, we can use number 9. So, using 9 – row * 1, we can find out how many spaces are needed for each row.

For the first row: we have 9 – 1*1 = 8, and for the 2nd row we have 9 – 2*1 =7, etc.
The last one is easy because the row number and the number of digits are the same.
Accordingly, we can write the code as follows.

public class DigitPattern{
public static void main(String[] args){
for(int rowNumber=1; rowNumber<=9; rowNumber++){
for(int i=1; i<=9-rowNumber*1; i++){
System.out.print(" ");
}
for(int i=1; i<=rowNumber; i++){
System.out.print(i);
}
System.out.println();
}
}
}

000111222333444555666777888999
000111222333444555666777888999
000111222333444555666777888999
000111222333444555666777888999
000111222333444555666777888999
000111222333444555666777888999
000111222333444555666777888999

Here is the code for another number pattern shown above.

I used 70 for the outer loop and 3 for the inner loop as the upper limit, but you can use whatever number.

As you can see, the inner loop repeats the numbers three times. Additionally, the if clause brakes the numbers in 7 chunks by using the modulus operator.

You can see the code below.

public static void main( String[] args) {
int number = 0;
for(int i=1; i<=70; i++){
for (int n = 0; n<= 2; n++){
System.out.print(number);
}
number = number + 1;
if (i%10==0){
System.out.println();
number = 0;
}
}
System.out.println();
}

}

1234567890|1234567890|1234567890|1234567890|

Another simple number pattern. In this case, the program produces digits from 1 to 10, but it changes the ’10’ to 0 as shown in the if clause.
The last line is not part of the loop. It is there to leave extra space.

public class Pattern2 {
public static void main( String[] args) {
int number = 1;
for(int i=1; i<=4; i++){
for (int j = 1; j<= 10; j++){
number = j;
if(number == 10)
number = 0;
System.out.print(number);
}
System.out.print("|");
}

System.out.println("\n");  // This is not part of the loop

}
}