Pyramid Star Patterns in C Programming

Udara Jayamini
5 min readMar 30, 2020

--

C programming language is a one of the most useful and fundamental programming language. Creating star patterns is a challenging task for programming beginners. However it will help you to improve your logical thinking . There can be various coding methods for the same star pattern because it is depends on a coders perception. In this article i will provide you the code for some of the the pyramid star patterns. One of the coding explanation is done for you.

#include<stdio.h>
void main()
{
int n;
printf("Enter the number of rows:");
scanf("%d",&n);
for(int i=1;i<=n;i++) //For loop for rows
{
for(int s=n;s>=i;s--) //For loop for print space
{
printf(" ");
}
for( int l=1;l<=(i*2-1);l++) //For loop for print star
{
printf("*");
}
printf("\n");
}
}

The output of the above coding is shown below.

Explanation-:

I)Get the user input for number of rows and store it in defined variable which is defined as ‘n’.

II) ‘i’ is a variable for count the number of rows and using for loop it will count until the value of ‘i’ equal to the ‘n’.

III)’s’ is a variable which use to print spaces & in the initialization ‘s’ is equal to the ‘n’.It will print star until ‘s’ is equal to the ‘i’.

IV)’l’ is a variable which use to print star & in the initialization 1 is assign to the ‘l’. It prints star until ‘l’ is equal to the i*2–1.

V)Then print a new line and go through for loop.

2.

#include<stdio.h>
void main()
{
int n, k=1;
printf("Enter number of rows: ");
scanf("%d",&n);
for(int i=1;i<=n;i++) //For loop for rows
{
for(int j=1;j<i;j++)// For loop for display spaces
{
printf(" ");
}
for(int s=n*2;s>k;s--)//For loop for display stars
{
printf("*");
}
k+=2;
printf("\n");
}
}

3.

#include<stdio.h>
void main()
{
int n;
printf("Enter the rows: ");
scanf("%d",&n);
for(int j=1;j<=n;j++) //For loop for rows
{
for(int k=j;k<n;k++) // For loop for spaces
{
printf(" ");
}
for(int h=1;h<=j;h++) // For loop for stars
{
printf("* ");
}
printf("\n");
}
}

4.

#include<stdio.h>
void main()
{
int n;
printf("Enter the rows: ");
scanf("%d",&n);
for(int j=1;j<=n;j++) //For loop for the rows
{
for(int k=1;k<=j;k++) //For loop for spaces
{
printf(" ");
}
for(int h=j;h<=n;h++) //For loop for star
{
printf("* ");
}
printf("\n");
}
}

5.

#include<stdio.h>
void main()
{
int n,h=2;
printf("Enter the rows: ");
scanf("%d",&n);
for (int m=1;m<=n;m++) //For loop for rows
{
for(int k=n*2;k>=m+h;k--) //For loop for spaces
{
printf(" ");
}
for (int j=1;j<=m;j++)
{
printf("*"); //For loop for stars
}
h++;
printf("\n");
}
}

6.

#include<stdio.h>
void main()
{
int n;
printf("Enter number of rows:");
scanf("%d",&n);
for(int i=1;i<=n;i++) //For loop for rows
{
for(int j=1;j<=(2*i-1);j++)// For loop for display spaces
{
printf(" ");
}
for(int s=i;s<=n;s++)//For loop for display stars
{
printf("*");
}
printf("\n");
}
}

7.

#include<stdio.h>
void main()
{
int n;
printf("Enter the rows:");
scanf("%d",&n);
for(int i=1;i<=n;i++)//For loop for rows
{
for(int s=1;s<=i;s++)//For loop for columns
{
printf("*");
}
printf("\n");
}
}

8.

#include<stdio.h>
void main()
{
int n;
printf("Enter the rows: ");
scanf("%d",&n);
for(int i=n;i>=1;i--) //For loop for rows
{
for(int s=1;s<=i;s++) //For loop for columns
{
printf("*");
}
printf("\n");
}
}

9.

#include<stdio.h>
void main()
{
int n;
printf("Enter number of rows: ");
scanf("%d",&n);
for(int i=n;i>=1;i--) //For loop for rows
{
for(int s=1;s<=i;s++)//For loop for spaces
{
printf(" ");
}
for(int j=n;j>=i;j--) //For loop for stars
{
printf("*");
}
printf("\n");
}
}

10.

#include<stdio.h>
void main()
{
int n;
printf("Enter number of rows: ");
scanf("%d",&n);
for(int i=1;i<=n;i++) //For loop for rows
{
for(int j=1;j<i;j++)// For loop for display spaces
{
printf(" ");
}
for(int s=i;s<=n;s++)//For loop for display stars
{
printf("*");
}
printf("\n");
}
}

11.

#include<stdio.h>
void main()
{
int n;
printf("Enter the rows(Odd No): ");
scanf("%d",&n);
for(int i=1;i<=(n+1)/2;i++)
{
for(int s=(n+1)/2;s>i;s--)
{
printf(" ");
}
for(int m=1;m<=i;m++)
{
printf("*");
}
printf("\n");
}
for(int j=1;j<(n+1)/2;j++)
{
for(int k=1;k<=j;k++)
{
printf(" ");
}
for(int h=(n-1)/2;h>=j;h--)
{
printf("*");
}
printf("\n");
}
}

12.

#include<stdio.h>
void main()
{
int n;
printf("Enter the rows(Odd No): ");
scanf("%d",&n);
for(int i=1;i<=(n+1)/2;i++)
{
for(int s=1;s<=i;s++)
{
printf("*");
}
printf("\n");
}
for(int j=1;j<(n+1)/2;j++)
{
for(int k=(n-1)/2;k>=j;k--)
{
printf("*");
}
printf("\n");
}
}

Thank you for reading.

--

--

Udara Jayamini
Udara Jayamini

Written by Udara Jayamini

Undergraduate of the university of moratuwa.

No responses yet