codescracker
c

C Program to Print Star and Pyramid Patterns



« Previous Program Next Program »

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:

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.

// Prints Half-Pyramid using Stars
// ------codescracker.com-----

#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 (1);
}

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:

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:

// Prints star pattern
// ------codescracker.com------

#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

Print First Half Pyramid using Stars

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

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

// Prints star pattern or half pyramid using star
// ------codescracker.com------

#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:

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

// Prints star pattern
// ------codescracker.com------

#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:

// Prints half pyramid using natural numbers
// ------codescracker.com------

#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:

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:

// ------codescracker.com------

#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 Pyramid using Star

This program prints pura 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:

// Prints complete pyramid pattern in C
// -------codescracker.com---------

#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 »



© Copyright 2021. All Rights Reserved.

CodesCracker