0% found this document useful (0 votes)
71 views3 pages

Pattern in C

The document provides C programs to print various pyramid patterns, including half pyramids of stars and numbers, an inverted half pyramid of stars, and a full pyramid of stars. Each example includes the corresponding C code and the expected output. The document serves as a tutorial for creating these patterns using loops in C programming.

Uploaded by

sohamrana7777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views3 pages

Pattern in C

The document provides C programs to print various pyramid patterns, including half pyramids of stars and numbers, an inverted half pyramid of stars, and a full pyramid of stars. Each example includes the corresponding C code and the expected output. The document serves as a tutorial for creating these patterns using loops in C programming.

Uploaded by

sohamrana7777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C Program to Print Pyramids and

Patterns
Example 1: Half Pyramid of *

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

C Program
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
return 0;
}

Output:
*
**
***
****
*****

Example 2: Half Pyramid of Numbers

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

C Program
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",i);
}
printf("\n");
}
return 0;
}
Output:
1
22
333
4444
55555

Example 3: Inverted half pyramid of *

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

C Program
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output:

*****
****
***
**
*
Another example:

Example 3: Full pyramid of *


#include <stdio.h>

int main() {
int n, i, j;
printf("How many rows do you want to enter in your pyramid?\n");
scanf("%d", &n);

for(i = 1; i <= n; i++) { // Loop through rows


for(j = 1; j <= 2 * n - 1; j++) { // Loop through columns
if(j >= n - (i - 1) && j <= n + (i - 1))
printf("*");
else
printf(" ");
}
printf("\n");
}

return 0;
}

You might also like