Write a C program to print a right triangle star pattern
Description: Print a right triangle of stars with 5 rows. The first row has one star, the second has two, and so on.
Hint:
- Use two loops. The outer loop is for rows and the inner loop is for stars in that row.
C program to print a right triangle of stars
#include <stdio.h>
int main(void) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output:
Output of the above program is
*
* *
* * *
* * * *
* * * * *
How it works
- The outer loop variable
iis the row number. - The inner loop prints
istars in rowi. printf("\n")moves to the next row.
Read C Nested Loops.
Learn more about the similar topics:
| Tutorials |
|---|
| C If Else Statement |
| C Else If Ladder and Nested If |
| C Switch Statement |
| C While Loop |
| C Do While Loop |
| C For Loop |
| C Nested Loops |