Inverted Mirrored Triangle Pattern

Understanding the Problem

The goal is to print an inverted mirrored triangle pattern where numbers decrement sequentially, and each row has a decreasing number of elements, with the numbers printed in reverse order.

Pattern Explanation

The pattern consists of rows where the numbers are printed in reverse order, starting from the highest number in the first row to the lowest number in the last row.

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

Observations:

  • The pattern consists of 4 rows.
  • Numbers decrement from left to right but are printed in reverse order in each row.
  • 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:

  1. Initialize a variable to determine the total number of rows (N).
  2. Calculate the starting number for the first row using the formula: start = (N * (N + 1)) / 2.
  3. Loop through rows (from N down to 1).
  4. For each row, print the numbers in reverse order starting from the calculated starting number.
  5. Print the number followed by '*' except for the last number in the row.
  6. Update the starting number for the next row by decrementing it by the current row number.
  7. 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 start = (n * (n + 1)) / 2;  // Calculate starting number for the first row

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

    return 0;
}
            

Output:

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