C Programs to Print Half Pyramid, Full Pyramid (Star and Number)

Here are the C Programs to print half pyramid, full pyramid, star pyramid, and number pyramid.

C Program to Print Pyramid

Various programs to print pyramid using C language.

Half Star Pyramid – C Program

Aim: Write a C program to print half star pyramid.

Program:


#include<stdio.h> 
int main() {
   int i, j, n;
   printf("Number of rows: ");
   scanf("%d", &n);
   for (i = 1; i <= n; ++i) {
      for (j = 1; j <= i; ++j) {
         printf("* ");
      }
      printf("\n");
   }
   return 0;
}

Output:

Number of rows: 5
*
* *
* * *
* * * *
* * * * *

Full Star Pyramid – C Program

Aim: Write a C program to print a full star pyramid.

Program:


#include<stdio.h> 
int main() {
   int i, space, rows, k = 0;
   printf("Number of rows: ");
   scanf("%d", &rows);
   for (i = 1; i <= rows; ++i, k = 0) {
      for (space = 1; space <= rows - i; ++space) {
         printf("  ");
      }
      while (k != 2 * i - 1) {
         printf("* ");
         ++k;
      }
      printf("\n");
   }
   return 0;
}

Output:

Number of rows: 5
        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *

Half Number Pyramid – C Program

Aim: Write a C program to print a half – number pyramid.

Program:


#include<stdio.h> 
int main() {
   int i, j, rows;
   printf("Number of rows: ");
   scanf("%d", &rows);
   for (i = 1; i <= rows; ++i) {
      for (j = 1; j <= i; ++j) {
         printf("%d ", j);
      }
      printf("\n");
   }
   return 0;
}

Output:

Number of rows: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Full Number Pyramid – C Program

Aim: Write a C program to print a full – number pyramid.

Program:


#include<stdio.h> 
int main() {
   int i, space, rows, k = 0, count = 0, count1 = 0;
   printf("Number of rows: ");
   scanf("%d", &rows);
   for (i = 1; i <= rows; ++i) {
      for (space = 1; space <= rows - i; ++space) {
         printf("  ");
         ++count;
      }
      while (k != 2 * i - 1) {
         if (count <= rows - 1) {
            printf("%d ", i + k);
            ++count;
         } else {
            ++count1;
            printf("%d ", (i + k - 2 * count1));
         }
         ++k;
      }
      count1 = count = k = 0;
      printf("\n");
   }
   return 0;
}

Output:

Number of rows: 5
        1
      2 3 2
    3 4 5 4 3
  4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5

Tagged in: