0% found this document useful (0 votes)
1 views11 pages

For Loop Programs

The document contains various C programming code snippets for generating different star patterns, including square, hollow square, triangles, pyramids, diamonds, and more. Each pattern is accompanied by its respective code and an example output. The document serves as a guide for understanding how to create these patterns using nested loops in C.

Uploaded by

Rama Krishna
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)
1 views11 pages

For Loop Programs

The document contains various C programming code snippets for generating different star patterns, including square, hollow square, triangles, pyramids, diamonds, and more. Each pattern is accompanied by its respective code and an example output. The document serves as a guide for understanding how to create these patterns using nested loops in C.

Uploaded by

Rama Krishna
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
You are on page 1/ 11

Square Star Pattern

#include <stdio.h>

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

printf("Enter the number of rows: ");


scanf("%d", &n);
for(i = 1; i <= n; i++)
{ // Outer loop for rows
for(j = 1; j <= n; j++)
{ // Inner loop for columns
printf("* ");
}
printf("\n"); // Move to the next line after each row
}
return 0;
}

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

\
Hollow Square Star Pattern
#include <stdio.h>
int main() {
int i, j, n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
for(j = 1; j <= n; j++) {
if(i == 1 || i == n || j == 1 || j == n)
printf("* "); // Print * for first and last rows/columns
else
printf(" "); // Print space for inner area
}
printf("\n");
}
return 0;
}
*****
* *
* *
* *
*****
Right-angled Triangle (Left-Aligned)
#include <stdio.h>

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

printf("Enter the number of rows: ");


scanf("%d", &n);

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


for(j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
*
**
***
****
*****

Right-angled Triangle (Right-Aligned)


#include <stdio.h>

int main() {
int i, j, space, n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
for(space = 1; space <= n - i; space++) {
printf(" "); // Print spaces
}
for(j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}

*
**
***
****
*****
Pyramid Pattern
#include <stdio.h>
int main() {
int i, j, space, n;
printf("Enter the number of rows: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
for(space = 1; space <= n - i; space++) {
printf(" "); // Print spaces for alignment
}
for(j = 1; j <= (2 * i - 1); j++) {
printf("*"); // Print stars
}
printf("\n");
}
return 0;
}
*
***
*****
*******
*********
\
Diamond Pattern
#include <stdio.h>
int main() {
int i, j, space, n;
printf("Enter the number of rows: ");
scanf("%d", &n);
// Upper half
for(i = 1; i <= n; i++) {
for(space = 1; space <= n - i; space++) {
printf(" ");
}
for(j = 1; j <= (2 * i - 1); j++) {
printf("*");
}
printf("\n");
}
// Lower half
for(i = n - 1; i >= 1; i--) {
for(space = 1; space <= n - i; space++) {
printf(" ");
}
for(j = 1; j <= (2 * i - 1); j++) {
printf("*");
}
printf("\n");
}
return 0;
}
*
***
*****
*******
*********
*******
*****
***
*
Hollow Right-Angled Triangle

#include <stdio.h>

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

printf("Enter the number of rows: ");


scanf("%d", &n);

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


for(j = 1; j <= i; j++) {
if (j == 1 || j == i || i == n)
printf("* ");
else
printf(" "); // Print spaces inside
}
printf("\n");
}

return 0;
}

*
**
* *
* *
*****
Hollow Pyramid
#include <stdio.h>

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

printf("Enter the number of rows: ");


scanf("%d", &n);

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


for(space = 1; space <= n - i; space++) {
printf(" ");
}
for(j = 1; j <= (2 * i - 1); j++) {
if (j == 1 || j == (2 * i - 1) || i == n)
printf("*");
else
printf(" ");
}
printf("\n");
}

return 0;
}

*
**
* *
* *
*********
Pascal’s Triangle

#include <stdio.h>

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

printf("Enter the number of rows: ");


scanf("%d", &n);

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


for(int space = 1; space <= n - i; space++)
printf(" ");
for(j = 0; j <= i; j++) {
if (j == 0 || i == 0)
coef = 1;
else
coef = coef * (i - j + 1) / j;
printf("%4d", coef);
}
printf("\n");
}

return 0;
}

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Butterfly Pattern
1. #include <stdio.h>
2. int main()
3. {
4. int i, j, n;
5. printf("Enter the number of rows: ");
6. scanf("%d", &n);
7. for(i = 1; i <= n; i++)
8. {
9. for(j = 1; j <= i; j++)
10. {
11. printf("*");
12. }
13. for(j = 1; j <= 2 * (n - i); j++)
14. {
15. printf(" ");
16. }
17. for(j = 1; j <= i; j++)
18. {
19. printf("*");
20. }
21. printf("\n");
22. }
23. for(i = n; i >= 1; i--)
24. {
25. for(j = 1; j <= i; j++)
26. {
27. printf("*");
28. }
29. for(j = 1; j <= 2 * (n - i); j++)
30. {
31. printf(" ");
32. }
33. for(j = 1; j <= i; j++)
34. {
35. printf("*");
36. }
37. printf("\n");
38. }
39. return 0;
40. }
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *

X Pattern

#include <stdio.h>

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

printf("Enter the number of rows: ");


scanf("%d", &n);

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


for(j = 1; j <= n; j++) {
if (j == i || j == (n - i + 1))
printf("*");
else
printf(" ");
}
printf("\n");
}

return 0;
}

* *
**
*
**
* *
Heart Pattern

#include <stdio.h>

int main() {
int i, j, n = 6; // Fixed size

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

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


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

return 0;
}

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

You might also like