Basic Incrementing Inverted Squared Number-Star Pattern
Understanding the Problem
The goal is to print an inverted squared number-star pattern where numbers increment sequentially but rows are printed in reverse order.
Pattern Explanation
The pattern follows a grid structure where numbers increase sequentially, but the last row appears first.
13*14*15*16
9*10*11*12
5*6*7*8
1*2*3*4
Observations:
- The pattern consists of 4 rows and 4 columns.
- Numbers increment from left to right.
- Each number is followed by a '*', except the last number in each row.
- The rows are printed in reverse order.
Algorithm
The pattern follows these steps:
- Initialize a number counter starting from 1.
- Store values in a 2D array as they increment.
- Loop through rows in reverse order.
- Within each row, print numbers left to right with '*'.
- Move to the next line after printing each row.
Method 1: Using Nested Loops
This method first stores values in an array and then prints them in reverse order.
public class InvertedNumberPattern {
public static void printPattern(int n) {
int[][] arr = new int[n][n];
int num = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = num++;
}
}
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j]);
if (j < n - 1) System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args) {
int size = 4;
printPattern(size);
}
}
Output:
13*14*15*16
9*10*11*12
5*6*7*8
1*2*3*4