C Program to Print Triangle and Pyramid patterns of Star(*) Character Using Loop

  • Write a C program to print triangle and pyramid star pattern.

This Program first takes the numbers of rows in pattern and then prints the corresponding pattern using nested for loops. This kind of problems are useful for beginners to understands the fundamentals of loops and spaces. Here, we will discuss about four variations of patterns using '*' character, right triangle, inverted right triangle, pyramid and inverted pyramid.

C program to print triangle pattern using * and loop

In this program, we first take the number of rows in the pattern as input from user using scanf function. Then we use two for loops to print triangle pattern. Outer for loop prints one horizontal row of pattern in one iteration whereas inner for loop prints n stars for nth row in one iteration.

/*
* C program to print triangle pyramid pattern using *
*/
#include<stdio.h>
#include<conio.h>

int main() {
    int i,j,rows;
    printf("Enter the number of rows\n");
    scanf("%d", &rows);
    
    for(i = 1; i <= rows; i++) {
    /* Prints one row of triangle */
        for(j = 1; j <= i; ++j) {
           printf("* ");
        }
        printf("\n");
    }
    getch();
    return 0;
}
Program Output
Enter the number of rows
6
*
* *
* * *
* * * *
* * * * *
* * * * * *

C program to print inverted triangle pattern using * and loop

This c program is similar to above program, the only difference is the pattern is inverted. For ith row we are printing (rows - i + 1) starts.

For Example
Let, total number of rows in pattern is 6 then.
Number of starts in 3th row = 6 - 3 + 1 = 4

/*
* C program to print inverted half pyramid pattern using *
*/
#include<stdio.h>
#include<conio.h>

int main() {
    int i,j,rows;
    printf("Enter the number of rows\n");
    scanf("%d", &rows);
    
    for(i = rows; i > 0; i--) {
    /* Prints one row of triangle */
        for(j = i; j > 0; j--) {
           printf("* ");
        }
        printf("\n");
    }
    getch();
    return 0;
}
Program Output
Enter the number of rows
6
* * * * * *
* * * * *
* * * *
* * * 
* *
*

C program to print pyramid pattern using * and loop

In this program, we are printing a pyramid pattern in which ith row contains (2*i - 1) space separated stars. We first take the number of rows in the pattern as input from user using scanf function. One iteration of outer for loop will print a row of pyramid. Inner for loop prints the initial spaces for every line and nested while loop prints (2*r - 1) space separated stars for rth row of pyramid.

/*
* C Program to print full pyramid pattern using *
*/
#include<stdio.h>
#include<conio.h>
int main() {
    int row, space, rows, star=0;
    printf("Enter the number of rows in pyramid\n");
    scanf("%d",&rows);

    for(row = 1;row <= rows; row++) {
     /* Printing spaces */
        for(space = 1; space <= rows-row; space++) {
           printf("  ");
        }
        /* Printing stars */
        while(star != (2*row - 1)) {
            printf("* ");
            star++;;
        }
        star=0;
        printf("\n");
    }
    getch();
    return 0;
}
Program Output
Enter the number of rows in pyramid
5 
       *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *

C program to print inverted pyramid pattern using * and loop

/*
* C Program to print full pyramid pattern using *
*/
#include<stdio.h>
#include<conio.h>
int main() {
    int row, space, rows, star=0;
    printf("Enter the number of rows in reverse pyramid\n");
    scanf("%d",&rows);

    for(row = rows;row >= 1; row--) {
     /* Printing spaces */
        for(space = 0; space <= rows-row; space++) {
           printf("  ");
        }
        /* Printing stars */
        star = 0;
        while(star != (2*row - 1)) {
            printf("* ");
            star++;
        }
        printf("\n");
    }
    getch();
    return 0;
}
Program Output
Enter the number of rows in reverse pyramid
5 
* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *

Related Topics
C program to print diamond star pattern
C Program to print rhombus star pattern
C program to print heart shape star pattern
C program to print hut shape star pattern
C program to print binary triangle pattern
C program to print hollow pyramid star pattern
C program to print hollow square star pattern
C program to print pascal triangle till N rows
C programs to print star triangle patterns
C Program to print a matrix diagonally
C program to print lower triangular matrix
List of all C programs