Java Code Example: Pattern Reverse

This is another example of a Java program that prints a pattern of numbers. The program is similar to the previous example, but it prints the numbers in reverse order for each row.

Code Example

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

Output

11111111
2222222
333333
44444
5555
666
77
8

Code Explanation

Here’s a breakdown of how the program works:

  • The outer loop iterates over the rows of the pattern, from 1 to 8.
  • The inner loop iterates over the columns of the pattern, from 8 to the current row number i.
  • For each position in the pattern, the value of i is printed using the System.out.print() method.
  • After printing all the values in the current row, a newline character is printed using the System.out.println() method to move to the next row.

As you can see, the pattern is the same as the previous example, but the numbers are printed in reverse order for each row.