For Loop Programs
For Loop Programs
#include <stdio.h>
int main() {
int i, j, n;
*****
*****
*****
*****
*****
\
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;
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;
return 0;
}
*
**
* *
* *
*****
Hollow Pyramid
#include <stdio.h>
int main() {
int i, j, space, n;
return 0;
}
*
**
* *
* *
*********
Pascal’s Triangle
#include <stdio.h>
int main() {
int i, j, n, coef = 1;
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;
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");
}
return 0;
}
*** ***
***** *****
*************
***********
*********
*******
*****
***
*