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