Inverted Incrementing Triangle Pattern
Understanding the Problem
The goal is to print an inverted triangle pattern where numbers increment sequentially, and each row has a decreasing number of elements.
Pattern Explanation
The pattern follows an inverted triangular structure where numbers increase sequentially from the bottom row to the top row.
7*8*9*10 4*5*6 2*3 1
Observations:
- The pattern consists of 4 rows.
- Numbers increment from left to right.
- Each row contains one less 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).
- Calculate the starting number for the first row as (N * (N + 1)) / 2.
- Loop through rows (from N down to 1).
- Within each row, loop through columns (up to the current row number).
- 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 InvertedIncrementingTrianglePattern { public static void main(String[] args) { int n = 4; // Number of rows int num = (n * (n + 1)) / 2; // Starting number for the first row // Loop to print the inverted incrementing triangle pattern for (int i = n; i >= 1; i--) { // Loop for each row for (int j = 1; j <= i; j++) { // Loop for each column in the current row System.out.print(num++); // Print the current number if (j < i) System.out.print("*"); // Print '*' if not the last number in the row } System.out.println(); // Move to the next line after each row } } }
Output:
7*8*9*10 4*5*6 2*3 1