pattern 7: Pyramid Star pattern program in C Language

Spread the love

Write a C Program to print below pattern ( Pyramid pattern ) :

*
* *
* * *
* * * *
* * * * *
* * * *
* * * 
* * 
*

Explanation :

This is one the series of pattern programs in C. This program accept one integer from the user and prints above triangle pattern( It prints pattern depending upon the number, above pattern is for n=4). Here I am using only two for loops to create above pattern. First for loop is responsible for the number of rows. so it always starts with -n. The inner loop is for columns. I am using small logic(using variable ‘k’) to accomplish this pattern with only two loops.

Program :

  1. #include<stdio.h>
  2. int main()
  3. {
  4.     int i,j,k,n;
  5.     printf(“Enter the Value for n : “);
  6.     scanf(“%d”,&n);
  7.  
  8.     for(i=-n;i<=n;i++) // Note that ‘i’ is starting from -n (negative n)
  9.     {
  10.         k=i;
  11.         if(k<0)
  12.             k= k*-1;
  13.  
  14.         for(j=0;j<=n;j++)
  15.         {
  16.             if(k<=j)
  17.                 printf(“* “);
  18.         }
  19.         printf(n);
  20.     }
  21.         return 0;
  22. }
Output:

Similar Star pattern programs:

More C programs:

C Tutorials with simple Examples:

Venkatesh Macha

Hi Guys, I am Venkatesh Macha. I am a programmer and an Open Source enthusiast, Presently working as a Software Engineer. Apart from that, I love to explore new technologies and things.

You may also like...

1 Response

Leave a Reply