Pyramid pattern in c
Today we are going to learn to print star pyramid patterns in c using for loop. To understand these very clearly you need to know about
If you have clear knowledge about the above concepts then move on to level 2.
Example 1.
#include<stdio.h>
int main()
{
int len=9;
int space=len/2;
for (int i=1; i<=len;i++)
{
//print spaces
for(int s=1;s<=space;s++)
{
printf(" ");
}
// print stars
for(int j=1;j<=i;j++)
{
printf(" * ");
}
printf("\n");
space--;
i++;
}
return 0;
}
output
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Example 2:
Write a program to print right angle star pattern in c
#include<stdio.h>
int main()
{
for (int i=1;i<=9;i++)
{
for(int j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
output:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
Example 3
write a program to print a square star pattern
#include<stdio.h>
int main()
{
for (int i=1;i<=9;i++)
{
for(int j=1;j<=9;j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
output
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * *
Example 3
Write a program in c to print mirrored right angle triangle
#include<stdio.h>
int main()
{
for (int i=1;i<=9;i++)
{
// print space=(n-i) where n=9
for(int j=1;j<=9-i;j++)
{
printf(" ");
}
for(int j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
output
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
Write a program to print an inverted right triangle star pattern
#include<stdio.h>
int main()
{
for (int i=1;i<=9;i++)
{
for(int j=9;j>=i;j--)
{
printf("* ");
}
printf("\n");
}
return 0;
}
output
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
0 Comments