BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to Print Pyramid Star Pattern

By Chaitanya Singh | Filed Under: C Programs

In this tutorial, you will learn how write a C program to print the Pyramid star pattern.

This is how a Pyramid star pattern looks like:

Number of rows = 7
Output: 
          *
         * *
        * * *
       * * * *
      * * * * *
     * * * * * *   
    * * * * * * * 
The first row of the pyramid contains one star, second row two stars and so on

Program to print Pyramid Star Pattern

#include <stdio.h>
int main()
{
  int row, column, numberOfRows = 6;
  for (row=0; row<numberOfRows; row++)
  {
    for (column=numberOfRows-row; column>1; column--)
    {
      printf(" ");
    }
    for (column=0; column<=row; column++ )
    {
      printf("* ");
    }
    // Move the cursor to new line for next row
    printf("\n");
  }
  return 0;
}

Output:
C Program to Print Pyramid Star Pattern
Explanation of the program:
In this program, we are using loops to print pyramid star pattern. There are three loops in this program. The outer loop runs the same number of times the number of rows in pattern. In this example, number of rows are 6 so this loop runs 6 times, it has two inner loops.

The first inner for loop is to print the white spaces in each row. The second inner for loop is to print the stars in each row. This loop is pretty simple, it prints one star for first row, two stars for second row and so on.

Practice the same program in java language here.

Related C Examples:

  • C Program to print Left Triangle Star pattern
  • C Program to print Right Triangle Star pattern
  • C Program to add two numbers
  • C Program to print an integer entered by user
❮ C TutorialC Programs ❯

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap