Printing Six Different Triangle Patterns in C

Pattern 1: Inverted Right Triangle

This pattern displays a right triangle with the right angle at the top-left corner. The number of asterisks decreases as we move down each row.

 1 #define _CRT_SECURE_NO_DEPRECATE
 2 #include <stdio.h>
 3 
 4 int main(void)
 5 {
 6     int height;
 7     int row, col;
 8     
 9     printf("Enter the triangle height: ");
10     scanf("%d", &height);
11 
12     for (row = 0; row <= height; row++)
13     {
14         for (col = height; col > row; col--)
15         {
16             putchar('*');
17         }
18         putchar('\n');
19     }
20     putchar('\n');
21     return 0;
22 }

Output Example (height = 5):




** *

Pattern 2: Right Triangle

This pattern creates a standard right triangle with the right angle at the bottom-left corner. The number of asterisks increases with each subsequent row.

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int height;
 6     int row, col;
 7     
 8     printf("Enter the triangle height: ");
 9     scanf("%d", &height);
10 
11     for (row = 0; row < height; row++)
12     {
13         for (col = 0; col <= row; col++)
14         {
15             putchar('*');
16         }
17         putchar('\n');
18     }
19     putchar('\n');
20     return 0;
21 }

Output Example (height = 5): * **




Pattern 3: Inverted Left-Aligned Triangle

This pattern displays an inverted right triangle that is aligned to the left side of the screen, with leading spaces pushing the asterisks toward the right.

 1 #define _CRT_SECURE_NO_DEPRECATE
 2 #include <stdio.h>
 3 
 4 int main(void)
 5 {
 6     int height;
 7     int row, col, offset;
 8     
 9     printf("Enter the triangle height: ");
10     scanf("%d", &height);
11 
12     for (row = 0; row < height; row++)
13     {
14         for (col = 0; col <= row; col++)
15         {
16             putchar(' ');
17         }
18         for (offset = height; offset > row; offset--)
19         {
20             putchar('*');
21         }
22         putchar('\n');
23     }
24     putchar('\n');
25     return 0;
26 }

Output Example (height = 5):




**
 *

Pattern 4: Left-Aligned Triangle

This pattern creates a right triangle aligned to the left, but with leading spaces that shift each row progressively to the right.

 1 #define _CRT_SECURE_NO_DEPRECATE
 2 #include <stdio.h>
 3 
 4 int main(void)
 5 {
 6     int height;
 7     int row, col, spaces;
 8     
 9     printf("Enter the triangle height: ");
10     scanf("%d", &height);
11 
12     for (row = 0; row < height; row++)
13     {
14         for (spaces = height; spaces > row; spaces--)
15         {
16             putchar(' ');
17         }
18         for (col = 0; col <= row; col++)
19         {
20             putchar('*');
21         }
22         putchar('\n');
23     }
24     putchar('\n');
25     return 0;
26 }

Output Example (height = 5):

*

**




Pattern 5: Isosceles Triangle

This pattern generates an isosceles triangle that is centered horizontal. Each row increases in width following an arithmetic progression.

 1 #define _CRT_SECURE_NO_DEPRECATE
 2 #include <stdio.h>
 3 
 4 int main(void)
 5 {
 6     int height;
 7     int row, col, leadingSpaces, stars;
 8     
 9     printf("Enter the triangle height: ");
10     scanf("%d", &height);
11 
12     for (row = 0; row < height; row++)
13     {
14         for (leadingSpaces = height; leadingSpaces > row; leadingSpaces--)
15         {
16             putchar(' ');
17         }
18         for (stars = 0; stars <= 2 * row; stars++)
19         {
20             putchar('*');
21         }
22         putchar('\n');
23     }
24     putchar('\n');
25     return 0;
26 }

Output Example (height = 5):





Pattern 6: Inverted Isosceles Triangle

This pattern creates an inverted isosceles triangle centered on the screen. The width decreases as we progress through each row.

 1 #define _CRT_SECURE_NO_DEPRECATE
 2 #include <stdio.h>
 3 
 4 int main(void)
 5 {
 6     int height;
 7     int row, col, trailingStars;
 8     int leadingSpaces = 0;
 9     
10     printf("Enter the triangle height: ");
11     scanf("%d", &height);
12 
13     for (row = 0; row < height; row++)
14     {
15         for (col = 0; col < leadingSpaces; col++)
16         {
17             putchar(' ');
18         }
19         for (trailingStars = 2 * height; trailingStars > 2 * row + 1; trailingStars--)
20         {
21             putchar('*');
22         }
23         putchar('\n');
24         leadingSpaces++;
25     }
26     putchar('\n');
27     return 0;
28 }

Output Example (height = 5):





*

Tags: C printf loops nested-loops asterisk-triangle

Posted on Wed, 13 May 2026 08:35:29 +0000 by zzz