Basic Incrementing Triangle Pattern

Understanding the Problem

The goal is to print a triangle pattern where numbers increment sequentially, and each row has an increasing number of elements.

Pattern Explanation

The pattern follows a triangular structure where numbers increase sequentially row by row.

1
2*3
4*5*6
7*8*9*10
            

Observations:

  • The pattern consists of 4 rows.
  • Numbers increment from left to right.
  • 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:

  1. Initialize a number counter starting from 1.
  2. Loop through rows (from 1 to N).
  3. Within each row, loop through columns (up to the row number).
  4. Print the number followed by '*' except for the last number in the row.
  5. Increment the number after each print.
  6. 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 IncrementingTrianglePattern {
    public static void main(String[] args) {
        int n = 4;  // Number of rows
        int num = 1;  // Starting number

        for (int i = 1; i <= n; 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:

1
2*3
4*5*6
7*8*9*10