C Programming/C Loop Statement

C Pattern Part 2 (Exercise)

Updated on January 9, 2026
2 min read

Pyramid Patterns (Star Patterns)

This topic is a step up from basic patterns and is very important for logic building.

Key Idea for Pyramid Patterns

Pyramid patterns use spaces + stars.

Rules:

  • Outer loop → rows
  • First inner loop → spaces
  • Second inner loop → stars

1. Simple Pyramid (Centered Star Pyramid)

Pattern

   *
  ***
 *****
*******

Logic

  • Rows = 4
  • Spaces = rows - i
  • Stars = 2*i - 1

Program 

c
1#include <stdio.h>
2
3int main() {
4    int i, j, k;
5
6    for (i = 1; i <= 4; i++) {
7        for (j = 1; j <= 4 - i; j++) {
8            printf(" ");
9        }
10        for (k = 1; k <= 2*i - 1; k++) {
11            printf("*");
12        }
13        printf("\n");
14    }
15
16    return 0;
17}

2. Inverted Pyramid

Pattern

*******
 *****
  ***
   *

Program

c
1#include <stdio.h>
2
3int main() {
4    int i, j, k;
5
6    for (i = 4; i >= 1; i--) {
7        for (j = 1; j <= 4 - i; j++) {
8            printf(" ");
9        }
10        for (k = 1; k <= 2*i - 1; k++) {
11            printf("*");
12        }
13        printf("\n");
14    }
15
16    return 0;
17}

3. Half Pyramid (Right Aligned)

Pattern

python
1
2   *
3  **
4 ***
5****

Program

c
1
2#include <stdio.h>
3
4int main() {
5    int i, j;
6
7    for (i = 1; i <= 4; i++) {
8        for (j = 1; j <= 4 - i; j++) {
9            printf(" ");
10        }
11        for (j = 1; j <= i; j++) {
12            printf("*");
13        }
14        printf("\n");
15    }
16    return 0;
17}

How to Solve Any Pyramid Pattern (Exam Formula)

  1. Count rows
  2. Identify spaces
  3. Identify stars
  4. Write loops in this order:
    1. spaces loop
    2. stars loop
C Pattern Part 2 (Exercise) | C Programming | Learn Syntax