Here we are going to write a c program to print pyramid pattern of stars. We will take number of rows input from the user so as to make our program user interactive. Star Pattern in C that we are going to print is given below -
Pattern -
*
* *
* * *
* * * *
* * * * *
C Program to Print Pyramid Pattern of Stars -
#include <stdio.h>
int main()
{
int n;
printf("Enter number of rows: ");
scanf("%d", &n);
for(int i=1; i<=n; i++)
{
for(int j=1; j<=i; j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
Output -
Enter number of rows: 5
*
* *
* * *
* * * *
* * * * *
Explanation of C Program to Print Pyramid Pattern of Stars -
First we are taking number of rows input from the user. Then running a nested loop to print out required pattern.
Nested loop algorithm -
Suppose n=5.
When i=1 , j=1 to 1 (as j<=i and i=1) so * will be printed.
When i=2, j=1 to 2 (as j<=i and i=2) so * * will be printed.
When i=3, j=1 to 3 (as j<=i and i=3) so * * * will be printed.
When i=4, j=1 to 4 (as j<=i and i=4) so * * * * will be printed.
When i=5, j=1 to 5 (as j<=i and i=5) so * * * * * will be printed.
So at the end the result that we are going to see on the screen is
*
* *
* * *
* * * *
* * * * *
Here after each j's loop the numbers are getting printed in next line because we are printing a new line after each j's loop i.e., this printf("\n") statement print new line.
Related Programs -

Post a Comment
If you have any doubts, please let me know