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:
- We define an integer variable
n
to specify the number of rows (5 in this case). - The outer
for
loop (i
) runs from 1 ton
to create rows. - The first inner
for
loop (j
) prints spaces (n - i
) to align numbers to the right. - The second inner
for
loop (k
) prints numbers sequentially from 1 toi
. 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:
- We declare
n
for the number of rows. - The outer loop (
i
) controls the rows from 1 ton
. - The first inner loop (
j
) prints spaces (n - i
) to align the numbers to the right. - The second inner loop (
k
) prints the row numberi
repeatedly. - 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:
- We declare
n
for the number of rows. - The outer loop (
i
) controls the rows from 1 ton
. - The first inner loop (
j
) prints spaces (n - i
) to align the numbers. - The second inner loop (
k
) prints decreasing numbers fromi
to 1. - 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:
- Incremental numbers: Sequentially increasing numbers.
- Repeating row numbers: Printing the same number in each row.
- Decreasing numbers: Printing numbers in reverse order.