Print a Mirrored Right-Angled Triangle Number Pattern in C

To print a mirrored right-angled triangle number pattern in C, we use nested loops: one for the rows and two for printing spaces and numbers. The pattern aligns numbers to the right by printing leading spaces before the numbers.


Examples to Print a Mirrored Right-Angled Triangle

1. Mirrored Right-Angled Triangle with Incremental Numbers

In this example, we print a mirrored right-angled triangle where numbers start from 1 and increase sequentially in each row.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int n = 5; // Number of rows

    for (int i = 1; i <= n; i++) {
        // Print spaces
        for (int j = 1; j <= n - i; j++) {
            printf(" ");
        }

        // Print numbers
        for (int k = 1; k <= i; k++) {
            printf("%d", k);
        }

        printf("\n");
    }

    return 0;
}

Explanation:

  1. We define an integer variable n to specify the number of rows (5 in this case).
  2. The outer for loop (i) runs from 1 to n to create rows.
  3. The first inner for loop (j) prints spaces (n - i) to align numbers to the right.
  4. The second inner for loop (k) prints numbers sequentially from 1 to i.
  5. printf("\n") moves to the next row.

Output:

    1
   12
  123
 1234
12345

2. Mirrored Right-Angled Triangle with Repeating Row Numbers

In this example, we modify the pattern so that each row contains the same number instead of increasing sequentially.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int n = 5; // Number of rows

    for (int i = 1; i <= n; i++) {
        // Print spaces
        for (int j = 1; j <= n - i; j++) {
            printf(" ");
        }

        // Print repeating numbers
        for (int k = 1; k <= i; k++) {
            printf("%d", i);
        }

        printf("\n");
    }

    return 0;
}

Explanation:

  1. We declare n for the number of rows.
  2. The outer loop (i) controls the rows from 1 to n.
  3. The first inner loop (j) prints spaces (n - i) to align the numbers to the right.
  4. The second inner loop (k) prints the row number i repeatedly.
  5. The printf("\n") ensures a new line for the next row.

Output:

    1
   22
  333
 4444
55555

3. Mirrored Right-Angled Triangle with Decreasing Numbers

In this example, we print decreasing numbers in each row, starting from the row number down to 1.

main.c

</>
Copy
#include <stdio.h>

int main() {
    int n = 5; // Number of rows

    for (int i = 1; i <= n; i++) {
        // Print spaces
        for (int j = 1; j <= n - i; j++) {
            printf(" ");
        }

        // Print decreasing numbers
        for (int k = i; k >= 1; k--) {
            printf("%d", k);
        }

        printf("\n");
    }

    return 0;
}

Explanation:

  1. We declare n for the number of rows.
  2. The outer loop (i) controls the rows from 1 to n.
  3. The first inner loop (j) prints spaces (n - i) to align the numbers.
  4. The second inner loop (k) prints decreasing numbers from i to 1.
  5. The printf("\n") ensures a new line for the next row.

Output:

    1
   21
  321
 4321
54321

Conclusion

In this tutorial, we explored different ways to print a mirrored right-angled triangle pattern using nested loops in C:

  1. Incremental numbers: Sequentially increasing numbers.
  2. Repeating row numbers: Printing the same number in each row.
  3. Decreasing numbers: Printing numbers in reverse order.