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:

  1. Initialize a number counter starting from 1.
  2. Determine the total number of rows (N).
  3. Loop through rows (from 1 to N).
  4. For each row, calculate the starting number for that row using the formula: start = (i * (i + 1)) / 2.
  5. Within each row, loop through columns (up to the row number) and print the numbers in reverse order.
  6. Print the number followed by '*' except for the last number in the row.
  7. Increment the number after each print.
  8. Move to the next line after printing each row.

Method 1: Using Nested Loops

This method uses two loops to print the pattern.

#include <stdio.h>

int main() {
    int n = 4;  // Number of rows
    int num = 1;  // Starting number

    // 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
            printf("%d", start - j);  // Print the current number in reverse order
            if (j < i - 1) printf("*");  // Print '*' if not the last number in the row
        }
        printf("\n");  // Move to the next line after each row
    }

    return 0;
}
            

Output:

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