0% found this document useful (0 votes)
2 views9 pages

C Patterns Programs

The document provides various C programming examples for generating different types of patterns, including star, number, alphabet, pyramid, hollow square, diamond, Pascal triangle, and butterfly patterns. Each pattern is accompanied by its respective code and output illustration. These examples serve as a guide for understanding pattern generation in programming.

Uploaded by

mani0007king
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)
2 views9 pages

C Patterns Programs

The document provides various C programming examples for generating different types of patterns, including star, number, alphabet, pyramid, hollow square, diamond, Pascal triangle, and butterfly patterns. Each pattern is accompanied by its respective code and output illustration. These examples serve as a guide for understanding pattern generation in programming.

Uploaded by

mani0007king
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

Star patterns

Number patterns
Alphabet patterns
Pyramid patterns
Hollow patterns
Diamond patterns
Pascal triangle
Butterfly pattern

1. Star Pattern
#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:
*
**
***
****
*****

2. Number Pattern
#include <stdio.h>

int main() {
int i, j;

for(i = 1; i <= 5; i++) {


for(j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}

return 0;
}
Output:
1
12
123
1234
12345

3. Alphabet Pattern
#include <stdio.h>

int main() {
int i, j;
char ch = 'A';

for(i = 1; i <= 5; i++) {


for(j = 1; j <= i; j++) {
printf("%c ", ch);
ch++;
}
printf("\n");
}

return 0;
}
Output:
A
BC
DEF
GHIJ
KLMNO
4. Pyramid Pattern
#include <stdio.h>

int main() {
int i, j, space;

for(i = 1; i <= 5; i++) {

for(space = 1; space <= 5 - i; space++) {


printf(" ");
}

for(j = 1; j <= (2*i - 1); j++) {


printf("*");
}

printf("\n");
}

return 0;
}
Output:
*
***
*****
*******
*********
5. Hollow Square Pattern
#include <stdio.h>

int main() {
int i, j;

for(i = 1; i <= 5; i++) {


for(j = 1; j <= 5; j++) {

if(i == 1 || i == 5 || j == 1 || j == 5)
printf("* ");
else
printf(" ");
}
printf("\n");
}

return 0;
}
Output:
*****
* *
* *
* *
*****
6. Diamond Pattern
#include <stdio.h>

int main() {
int i, j, space;

for(i = 1; i <= 5; i++) {

for(space = 1; space <= 5 - i; space++)


printf(" ");

for(j = 1; j <= (2*i - 1); j++)


printf("*");

printf("\n");
}

for(i = 4; i >= 1; i--) {

for(space = 1; space <= 5 - i; space++)


printf(" ");

for(j = 1; j <= (2*i - 1); j++)


printf("*");

printf("\n");
}
return 0;
}

7. Pascal Triangle
#include <stdio.h>

int main() {
int i, j, n = 5, coef = 1;

for(i = 0; i < n; i++) {


coef = 1;

for(j = 0; j <= i; j++) {

printf("%d ", coef);

coef = coef * (i - j) / (j + 1);


}

printf("\n");
}

return 0;
}
Output:
1
11
121
1331
14641

8. Butterfly Pattern
#include <stdio.h>

int main() {
int i, j, n = 5;

for(i = 1; i <= n; i++) {

for(j = 1; j <= i; j++)


printf("*");

for(j = 1; j <= 2*(n-i); j++)


printf(" ");

for(j = 1; j <= i; j++)


printf("*");

printf("\n");
}

for(i = n; i >= 1; i--) {

for(j = 1; j <= i; j++)


printf("*");
for(j = 1; j <= 2*(n-i); j++)
printf(" ");

for(j = 1; j <= i; j++)


printf("*");

printf("\n");
}

return 0;
}

You might also like