Mirrored Incrementing Triangle Pattern
Understanding the Problem
The goal is to print a mirrored triangle pattern where numbers increment sequentially, and each row has an increasing number of elements, but the numbers in each row are printed in reverse order.
Pattern Explanation
The pattern consists of rows where the numbers are printed in reverse order, starting from the last number in the row to the first.
1 3*2 6*5*4 10*9*8*7
Observations:
- The pattern consists of 4 rows.
- Numbers increment from left to right but are printed in reverse order in each row.
- Each row contains one more element than the previous row.
- Each number is followed by a '*', except the last number in each row.
Algorithm
The pattern follows these steps:
- Initialize a number counter starting from 1.
- Determine the total number of rows (N).
- Loop through rows (from 1 to N).
- For each row, calculate the starting number for that row using the formula:
start = (i * (i + 1)) / 2
. - Within each row, loop through columns (up to the row number) and print the numbers in reverse order.
- Print the number followed by '*' except for the last number in the row.
- Increment the number after each print.
- Move to the next line after printing each row.
Method 1: Using Nested Loops
This method uses two loops to print the pattern.
public class MirroredIncrementingTrianglePattern { public static void main(String[] args) { int n = 4; // Number of rows // Loop to print the mirrored incrementing triangle pattern for (int i = 1; i <= n; i++) { // Loop for each row int start = (i * (i + 1)) / 2; // Calculate starting number for the current row for (int j = 0; j < i; j++) { // Loop for each column in the current row System.out.print(start - j); // Print the current number in reverse order if (j < i - 1) System.out.print("*"); // Print '*' if not the last number in the row } System.out.println(); // Move to the next line after each row } } }
Output:
1 3*2 6*5*4 10*9*8*7