C Program to Print Star and Pyramid Patterns

In this article, you will learn and get code about how to print some famous patterns in C using stars and numbers like half pyramid pattern, full pyramid pattern. List of some famous patterns given in this article are:

  • Half pyramid pattern using stars
  • Half pyramid pattern using numbers
  • Pattern of Stars
  • Full pyramid pattern using stars

Pattern Programs in C

To print patterns of numbers and stars in C programming, you have to use two for loop. The outer for loop and the inner for loop. The outer for loop is responsible for rows and the inner for loop is responsible for columns.

Here are one by one C programs to print different-different patterns:

Print Half-Pyramid using Stars

This program prints half pyramid using star pattern.

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, j;
    for(i=0; i<5; i++)
    {
        for(j=0; j<=i; j++)
            printf("* ");
        printf("\n");
    }
    getch();
    return 0;
}

As this program is written under Code::Blocks IDE, therefore here is the snapshot of the sample run:

c program print patterns

Program Explained

  1. Initialize any two integer variable say i and j
  2. Here variable i works for rows and variable j works for columns
  3. It means that if you increment the value of variable i, the operation goes to next row
  4. And if you increment the value of variable j, the operation goes to next column
  5. Be sure to break the line, or use line break statement (printf("\n");) before going to next row
  6. So here initially the value of variable i gets initialized with 0 and 0 is obviously less than 5, therefore the condition evaluates to be true, then program flow goes inside the outer for loop
  7. The value of variable j gets initialized with 0, and checks whether the value of j (that is 0) is either less than or equal to value of i (that is 0) or not
  8. The condition evaluates to be true, therefore program flow goes inside the inner for loop, and one * (star) gets printed
  9. Again the program flow goes to the third statement of inner for loop, there the value of j gets incremented and becomes 1
  10. Again the program flow goes to the second statement of inner for loop and checks whether j<=i (1<=0) or not, this time condition evaluates to be false
  11. So program flow does not goes inside the inner for loop, rather it goes to the third statement of outer for loop
  12. There the value of i gets incremented (like 0++) and the condition i<5 evaluates to be true as 1<5 is true, then program flow again goes inside the outer for loop
  13. Now start from the 7th step (with updated value of i) and process the code until the condition of outer for loop evaluates to be false

Print Star Pattern

This program prints star pattern in following way:

  • One star in first row
  • Three star in second row
  • Five star in third row
  • Seven star in fourth row
  • Nine star in fifth row

Here all the first star of every first column in each row represented in one vertical line. Let's take a look at the program for better understanding:

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, j, k=1;
    for(i=0; i<5; i++)
    {
        for(j=0; j<k; j++)
            printf("* ");
        k=k+2;
        printf("\n");
    }
    getch();
    return 0;
}

When the above C program is compile and executed, it will produce the following result:

print pattern in c programming

Program Explained

  • Here we have initialized one extra variable say k with 1 as its initial value
  • And instead of comparing j with i, we have compared it with k this time
  • Because to print 1, 3, 5, 7, and 9 star in first, second, third, fourth and fifth row
  • We have incremented the value of k with 2 each time after exiting from the inner for loop's statement
  • So that, at first time j (holds 0 initially) gets compared with 1 at first time
  • At second time j (holds 0 initially) gets compared with 3 at second time when program flow goes to inner for loop after outer one. Here initially o<3 evaluates to be true, so program flow goes inside inner for loop and statement gets executed, value of j gets incremented and becomes 1, compared with k (holds 3), so condition 1<3 evaluates to be true, again program flow goes inside inner for loop and its statement gets executed, again value of j gets incremented and becomes 2, compared with k or checks the condition j<k or (2<3) evaluates to be true, program flow goes inside the inner for loop again, executes its statement, again value of j gets incremented and becomes 3, now this time the condition 3<3 evaluates to be false, so the program flow goes to increment part of outer for loop and continue the program until the condition of outer for loop evaluates to be false
  • At third time j (holds 0 initially) gets compared with 5, each run of inner for loop
  • At fourth time j (holds 0 initially) gets compared with 7, each run of inner for loop
  • At fifth time j (holds 0 initially) gets compared with 9, each run of inner for loop
  • For example, first time, inner for loop's statement gets executed only one time as the value of k holds 1 initially
  • Second time, inner for loop's statement gets executed 3 times. Because its condition j<k evaluates to be true three times as j<k (0<3), j<k (1<3), and j<k (2<3) evaluates to be true
  • In this way 1, 3, 5, 7, and 9 stars gets printed on first, second, third, fourth, and fifth row as shown in the snapshot given above

Print First Half Pyramid using Stars

This program prints first half pyramid using star pattern in this way:

  • 8 space and 1 star in first row
  • 6 space and 2 star in second row
  • 4 space and 3 star in third row
  • 2 space and 4 star in fourth row
  • 0 space and 5 star in fifth row

Use space after each star. Let's take a look at the program:

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, j, space=8;
    for(i=0; i<5; i++)
    {
        for(j=0; j<space; j++)
            printf(" ");
        space = space-2;
        for(j=0; j<=i; j++)
            printf("* ");
        printf("\n");
    }
    getch();
    return 0;
}

Here is the snapshot of sample run:

c program print star pattern

Before printing star on each row, first print spaces. That is 8, 6, 4, 2, and 0 space on first, second, third, fourth, and fifth row. And 1, 2, 3, 4, and 5 star (after spaces) on each row.

Print Pattern of Stars

This program prints star pattern in following ways:

  • 16 spaces and 1 star
  • 12 spaces and 3 star
  • 8 spaces and 5 star
  • 4 spaces and 7 star
  • 0 space and 9 star

Use space after each star. Let's take a look at the program given here:

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, j, space=16, k=1;
    for(i=0; i<5; i++)
    {
        for(j=0; j<space; j++)
            printf(" ");
        space = space-4;
        for(j=0; j<k; j++)
            printf("* ");
        k = k+2;
        printf("\n");
    }
    getch();
    return 0;
}

The snapshot of the sample run of above program is given here:

print star pattern in c programming

This program is little bit similar to the above one. Except here we have to print 16 spaces at first and decrement spaces by 4 each time. And print 1 star at first and increment the value of star by 2 each time when going to the next row.

Print Half Pyramid using Natural Numbers

This program prints half pyramid using natural numbers:

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, j, num=1;
    for(i=0; i<5; i++)
    {
        for(j=0; j<=i; j++)
        {
            printf("%d ", num);
            num++;
        }
        printf("\n");
    }
    getch();
    return 0;
}

Here is the screenshot of sample run:

looping in c programming

As you can see from the above program, the code for both the loop is same as the very first program of this article, except that here we have used another variable num so that after initializing it with 1, we have incremented it by 1, each time to print natural number that starts from 1. Without mattering about rows and columns here.

Print Half Pyramid using Natural Numbers on Each Row

This program also prints half pyramid using natural number but not as done in previous program. This program prints patter in following ways:

  • 1 on first row
  • 1 2 on second row
  • 1 2 3 on third row
  • 1 2 3 4 on fourth row
  • 1 2 3 4 5 on fifth row

As you can clearly see, here we have to initialize the value of num with 1 each time before entering the inner for loop, that is responsible for columns. So that to print natural number starts from 1 on each and every row. Here is the program:

#include<stdio.h>
#include<conio.h>
int main()
{
    int i, j, num=1;
    for(i=0; i<5; i++)
    {
        num = 1;
        for(j=0; j<=i; j++)
        {
            printf("%d ", num);
            num++;
        }
        printf("\n");
    }
    getch();
    return 0;
}

Let's take a look at the sample output of above program as given in the snapshot here:

c program print pattern using looping

Print Full Pyramid using Star

This program prints pure pyramid pattern using star. And here user decides the height of pyramid at run-time.

That is, following C program asks from user to enter the number of rows to print pyramid of stars:

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

When you execute the above program at your end, then here is the snapshot of initial output you will see on your output screen:

c program print star pyramid

Now enter any number of rows say 8 and press ENTER key to see the output as shown in snapshot given here:

pyramid star pattern in c

Same Program in Other Languages

C Online Test


« Previous Program Next Program »