Star Triangle Pattern in C | Pattern Program Tutorial
Pattern printing is a classic exercise in C programming that helps beginners understand how loops and nested logic work. One of the most popular patterns is the star triangle pattern, where asterisks (*) are printed in a structured way to form triangular shapes. This tutorial explains how to print different star triangle patterns using nested loops in C.
Why Learn Pattern Printing?
Pattern programs are not just academic exercises — they help you understand nested loops, logic formulation, and control of output format. These skills are applicable in real‑world programming when generating structured text, debugging output formats, or handling multi‑level iterations.
Basic Logic of Star Triangle Pattern
Star triangle patterns are printed using **nested for loops** where the outer loop controls rows and the inner loops control printing of stars and spaces. By adjusting the number of stars and spaces in each row, you can form various shapes such as right‑angled and equilateral triangles. :contentReference[oaicite:1]{index=1}
1. Right‑Angled Star Triangle Pattern
This pattern prints stars in a right triangle shape. Each row contains one more star than the previous row.
#include <stdio.h>
int main() {
int rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output for 5 rows: * * * * * * * * * * * * * * *
2. Inverted Right‑Angled Triangle Pattern
This pattern prints stars starting from the maximum number in the first row and decreases in each subsequent row. :contentReference[oaicite:2]{index=2}
#include <stdio.h>
int main() {
int rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Sample output for 5 rows: * * * * * * * * * * * * * * *
3. Pyramid (Equilateral Triangle) Star Pattern
To form a symmetrical triangle (pyramid), we print spaces and stars in each row such that the stars are centered. :contentReference[oaicite:3]{index=3}
#include <stdio.h>
int main() {
int rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (int i = 1; i <= rows; i++) {
// Print leading spaces
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
// Print stars
for (int k = 1; k <= (2 * i - 1); k++) {
printf("*");
}
printf("\n");
}
return 0;
}
Sample output for 5 rows:
*
***
*****
*******
*********
4. Mirrored Right Triangle Pattern
A mirrored right triangle pattern shifts the stars to the right by printing spaces first. :contentReference[oaicite:4]{index=4}
#include <stdio.h>
int main() {
int rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (int i = 1; i <= rows; i++) {
// Print leading spaces
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
// Print stars
for (int k = 1; k <= i; k++) {
printf("*");
}
printf("\n");
}
return 0;
}
Sample output for 5 rows:
*
**
***
****
*****
Tips to Understand Pattern Logic
- Use the outer loop to iterate through the rows.
- Use inner loops to control printing of stars and spaces separately.
- The number of leading spaces determines the alignment of the pattern.
- Nested loops help control how many stars appear in each row.
Practice Exercises
- Print a hollow triangle pattern using stars.
- Print a diamond pattern using stars.
- Print number patterns instead of star patterns.
- Print patterns using alphabets like A, B, C.
Conclusion
Star triangle patterns are a fun and effective way to learn how loops and nested logic work in C programming. Practice with different variations to improve your understanding of loop structures and output formatting in C.
Codecrown