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 the half-pyramid pattern and the full-pyramid pattern. A list of some famous patterns given in this article are:

Pattern Programs in C

To print patterns of numbers and stars in C programming, you have to use two for loops, 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 patterns:

Print the half-pyramid using stars in C

This program prints a half-pyramid using a 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 in the Code::Blocks IDE, here is the snapshot of the sample run:

c program print patterns

Program Explained

  1. Initialize any two integer variables, 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 the next row.
  4. And if you increment the value of variable j, the operation goes to the next column.
  5. Before moving on to the next row, make sure to break the line or use a line break statement printf("\n");
  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 true, then program flow goes inside the outer for loop.
  7. The value of variable j gets initialized with 0, and it is checked whether the value of j (which is 0) is either less than or equal to the value of i (which is 0) or not.
  8. The condition evaluates to 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 the inner for loop, where the value of j gets incremented and becomes 1.
  10. Again, the program flow goes to the second statement of the inner for loop and checks whether j<=i (1<=0) or not; this time, the condition evaluates to false.
  11. So program flow does not go inside the inner for loop, but rather it goes to the third statement of the outer for loop.
  12. There, the value of i gets incremented (like 0++), and the condition i<5 evaluates to true as 1<5 is true, then program flow again goes inside the outer for loop.
  13. Now start from the 7th step (with the updated value of i) and process the code until the condition of the outer for loop evaluates to false.

Print the star pattern in C

This program prints the star pattern in the following way:

One vertical line represents the first asterisk of each first column in each row. 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 compiled and executed, it will produce the following result:

print pattern in c programming

Program Explained

The following program prints the first half of a pyramid with a star pattern in the following manner:

After each asterisk, add a space. 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 a snapshot of the sample run:

c program print star pattern

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

The following program prints the star pattern in the following ways:

After each asterisk, add a space. 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;
}

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

print star pattern in c programming

This program is a little bit similar to the above one. Except in this case, we have to print 16 spaces at first and then decrease them by 4 each time. And print 1 star at first, then increment the value of the star by 2 each time you go to the next row.

Print the half pyramid using natural numbers in C

This program prints a 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 the sample run:

looping in c programming

As you can see from the above program, the code for both the loops is the same as the very first program in 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 a natural number that starts at 1. Without regard for rows and columns in this case.

Print the half pyramid using natural numbers on each row in C

This program also prints a half-pyramid using natural numbers, but not as in the previous program. This program prints patterns in the following ways:

As you can see, we must initialize the value of num with 1 each time before entering the inner for loop, which is in charge of columns. To print a natural number, begin at 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 the above program as given in the snapshot here:

c program print pattern using looping

Print the full pyramid using stars in C

This program prints a pure pyramid pattern using a star. And here the user decides the height of the pyramid at runtime. That is, it asks the user to enter the number of rows to print a pyramid of stars, as instructed by the C program:

#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 a snapshot of the initial output you will see on your output screen:

c program print star pyramid

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

pyramid star pattern in c

The same program in different languages

C Quiz


« Previous Program Next Program »


Liked this post? Share it!