⓲C Program to Print Right Star Triangle
Program: Right Star Triangle
#include <stdio.h>
int main()
{
int i, j;
// Outer loop controls the number of rows
for(i = 1; i <= 5; i++)
{
// Inner loop prints stars in each row
for(j = 1; j <= i; j++)
{
printf("* ");
}
// Move to next line after each row
printf("\n");
}
return 0;
}
Output
*
* *
* * *
* * * *
* * * * *
Explanation
1️⃣ Header File
#include <stdio.h>
This line allows us to use printf() for printing output.
2️⃣ Main Function
int main()
The program execution starts from the main() function.
3️⃣ Variable Declaration
int i, j;
-
i → controls rows
-
j → controls number of stars in each row
4️⃣ Outer Loop (Rows)
for(i = 1; i <= 5; i++)
This loop decides how many rows will print.
| i value | Row |
|---|---|
| 1 | Row 1 |
| 2 | Row 2 |
| 3 | Row 3 |
| 4 | Row 4 |
| 5 | Row 5 |
5️⃣ Inner Loop (Stars)
for(j = 1; j <= i; j++)
This loop prints stars in each row.
| Row (i) | Stars Printed |
|---|---|
| 1 | * |
| 2 | * * |
| 3 | * * * |
| 4 | * * * * |
| 5 | * * * * * |
Because j runs until i, the stars increase each row.
6️⃣ New Line
printf("\n");
After printing stars in one row, this moves the cursor to the next line.
Simple Logic
Row 1 → 1 star
Row 2 → 2 stars
Row 3 → 3 stars
Row 4 → 4 stars
Row 5 → 5 stars
✅ Tip for exams:
Remember this rule for right triangle patterns:
Inner loop limit = row number✅ Next Program: Printing a Reverse Triangle Star Pattern in C using loop
Comments
Post a Comment