Print an Inverted Right-Angled Triangle Star Pattern in C
To print an inverted right-angled triangle star pattern in C, we use nested loops. The outer loop controls the number of rows, and the inner loop prints stars (*) based on the current row index. The number of stars decreases as we move to the next row, forming an inverted triangle.
Examples to Print an Inverted Right-Angled Triangle Star Pattern
1. Basic Inverted Right-Angled Triangle Pattern
In this example, we will print a simple inverted right-angled triangle where the number of stars in each row decreases from n to 1.
main.c
#include <stdio.h>
int main() {
int n = 5; // Number of rows
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Explanation:
- We declare an integer variable
nand assign it the value5(number of rows). - The outer loop (
for (int i = n; i >= 1; i--)) runs fromnto1, controlling the rows. - The inner loop (
for (int j = 1; j <= i; j++)) printsistars in each row. printf("* ")prints a star with space.printf("\n")moves to the next line after printing stars.
Output:
* * * * *
* * * *
* * *
* *
*
2. Inverted Right-Angled Triangle with User Input
In this example, we allow the user to input the number of rows, making the pattern dynamic.
main.c
#include <stdio.h>
int main() {
int n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Explanation:
- We declare an integer variable
nto store user input. printf("Enter the number of rows: ")prompts the user for input.scanf("%d", &n)reads the user input.- The outer loop (
for (int i = n; i >= 1; i--)) controls the number of rows. - The inner loop (
for (int j = 1; j <= i; j++)) prints stars in decreasing order. printf("\n")ensures the next row starts on a new line.
Output:
When the program is run, prompt appears to enter the number of rows. Let us say that we enter 4.
Enter the number of rows: 4
* * * *
* * *
* *
*
3. Right-Aligned Inverted Right-Angled Triangle Pattern
In this example, we will print an inverted right-angled triangle where the stars are right-aligned. This is achieved by printing leading spaces before the stars in each row.
main.c
#include <stdio.h>
int main() {
int n = 5; // Number of rows
for (int i = n; i >= 1; i--) {
// Print leading spaces
for (int j = 0; j < n - i; j++) {
printf(" "); // Two spaces for alignment
}
// Print stars
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n"); // Move to next line
}
return 0;
}
Explanation:
- We declare an integer variable
nand set it to5(number of rows). - The outer loop (
for (int i = n; i >= 1; i--)) runs fromnto1, controlling the rows. - The first inner loop (
for (int j = 0; j < n - i; j++)) prints spaces before the stars, ensuring right alignment. - The second inner loop (
for (int j = 1; j <= i; j++)) prints stars in each row. printf(" ")prints two spaces for alignment.printf("\n")moves to the next line after printing spaces and stars.
Output:
* * * * *
* * * *
* * *
* *
*
Conclusion
In this tutorial, we learned how to print an inverted right-angled triangle pattern in C:
- Basic star pattern: Used nested loops to print an inverted right-angled triangle.
- User input pattern: Allowed dynamic pattern printing based on user input.
- Number pattern: Printed numbers instead of stars in decreasing order.
