Pattern using for loop 1 to 20

1. Pattern 1 - Right Triangle

#include <stdio.h> int main() { int rows, i, j; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; i++) { for (j = 1; j <= i; j++) { printf("*"); } printf("\n"); } return 0; }

Output:

Enter the number of rows: 5 * ** *** **** *****


2. Pattern 2 - Inverted Right Triangle

#include <stdio.h> int main() { int rows, i, j; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) { printf("*"); } printf("\n"); } return 0; }

Output:

Enter the number of rows: 5 ***** **** *** ** *

3. Pattern 3 - Pyramid

#include <stdio.h> int main() { int rows, i, j, space; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; i++) { for (space = 1; space <= rows - i; space++) { printf(" "); } for (j = 1; j <= 2 * i - 1; j++) { printf("*"); } printf("\n"); } return 0; }

Output:

Enter the number of rows: 5 * *** ***** ******* *********

4. Pattern 4 - Inverted Pyramid

#include <stdio.h> int main() { int rows, i, j, space; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = rows; i >= 1; i--) { for (space = 1; space <= rows - i; space++) { printf(" "); } for (j = 1; j <= 2 * i - 1; j++) { printf("*"); } printf("\n"); } return 0; }

Output:

Enter the number of rows: 5 ********* ******* ***** *** *

5. Pattern 5 - Hollow Rectangle

#include <stdio.h> int main() { int rows, columns, i, j; printf("Enter the number of rows: "); scanf("%d", &rows); printf("Enter the number of columns: "); scanf("%d", &columns); for (i = 1; i <= rows; i++) { for (j = 1; j <= columns; j++) { if (i == 1 || i == rows || j == 1 || j == columns) printf("*"); else printf(" "); } printf("\n"); } return 0; }

Output:

Enter the number of rows: 4 Enter the number of columns: 6 ****** * * * * ******

6. Pattern 6 - Diamond

#include <stdio.h> int main() { int n, i, j; printf("Enter the number of rows: "); scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= n - i; j++) { printf(" "); } for (j = 1; j <= 2 * i - 1; j++) { printf("*"); } printf("\n"); } for (i = n - 1; i >= 1; i--) { for (j = 1; j <= n - i; j++) { printf(" "); } for (j = 1; j <= 2 * i - 1; j++) { printf("*"); } printf("\n"); } return 0; }

Output:

Enter the number of rows: 4 * *** ***** ******* ***** *** *

7. Pattern 7 - Hollow Diamond

#include <stdio.h> int main() { int n, i, j; printf("Enter the number of rows: "); scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= n - i; j++) { printf(" "); } for (j = 1; j <= 2 * i - 1; j++) { if (j == 1 || j == 2 * i - 1 || i == n) printf("*"); else printf(" "); } printf("\n"); } for (i = n - 1; i >= 1; i--) { for (j = 1; j <= n - i; j++) { 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; }

Output:

Enter the number of rows: 5 * * * * * * * * * * * * * * * * * *

Pattern 8 - Number Pyramid

#include <stdio.h> int main() { int rows, i, j, num = 1; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; i++) { for (j = 1; j <= i; j++) { printf("%d ", num); num++; } printf("\n"); } return 0; }

Output:

Enter the number of rows: 4 1 2 3 4 5 6 7 8 9 10

9. Pattern 9 - Alphabet Triangle

#include <stdio.h> int main() { int rows, i, j; char ch = 'A'; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; i++) { for (j = 1; j <= i; j++) { printf("%c ", ch); ch++; } printf("\n"); } return 0; }

Output:

Enter the number of rows: 4 A B C D E F G H I J

10. Pattern 10 - Pascal's Triangle

#include <stdio.h> int main() { int rows, coef = 1; printf("Enter the number of rows: "); scanf("%d", &rows); for (int i = 0; i < rows; i++) { for (int space = 1; space <= rows - i; space++) { printf(" "); } for (int 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; }

Output:

Enter the number of rows: 5 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

11. Pattern 11 - Right Angle Number Triangle

#include <stdio.h> int main() { int rows, i, j, num = 1; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; i++) { for (j = 1; j <= i; j++) { printf("%d ", num); num++; } printf("\n"); } return 0; }

Output:

Enter the number of rows: 4 1 2 3 4 5 6 7 8 9 10

12. Pattern 12 - Hollow Rectangle with Numbers

#include <stdio.h> int main() { int rows, columns, i, j, num = 1; printf("Enter the number of rows: "); scanf("%d", &rows); printf("Enter the number of columns: "); scanf("%d", &columns); for (i = 1; i <= rows; i++) { for (j = 1; j <= columns; j++) { if (i == 1 || i == rows || j == 1 || j == columns) printf("%d", num); else printf(" "); num++; } printf("\n"); } return 0; }

Output:

Enter the number of rows: 4 Enter the number of columns: 6 123456 1 12 1 18

123456

13. Pattern 13 - Half Diamond with Numbers

#include <stdio.h> int main() { int n, i, j, num = 1; printf("Enter the number of rows: "); scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= i; j++) { printf("%d ", num); num++; } printf("\n"); } for (i = n - 1; i >= 1; i--) { for (j = 1; j <= i; j++) { printf("%d ", num); num++; } printf("\n"); } return 0; }

Output:

Enter the number of rows: 4 1 2 3 4 5 6 7 8 9 10 7 8 9 10 4 5 6 2 3 1

14. Pattern 14 - Zigzag Pattern

#include <stdio.h> int main() { int n, i, j, k; printf("Enter the number of rows: "); scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j < i; j++) { printf(" "); } for (k = i; k <= n; k++) { printf("* "); } printf("\n"); } return 0; }

Output:

Enter the number of rows: 5 * * * * * * * * * * * * * * * * * * * * * * * * *

15. Pattern 15 - Right Triangle of Alphabets

#include <stdio.h> int main() { int rows, i, j; char ch = 'A'; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; i++) { for (j = 1; j <= i; j++) { printf("%c ", ch); ch++; } printf("\n"); } return 0; }

Output:

Enter the number of rows: 4 A B C D E F G H I J

16. Pattern 16 - Inverted Right Triangle of Alphabets

#include <stdio.h> int main() { int rows, i, j; char ch = 'A'; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = rows; i >= 1; i--) { for (j = 1; j <= i; j++) { printf("%c ", ch); ch++; } printf("\n"); } return 0; }

Output:

Enter the number of rows: 4 D E F G A B C D E A B C D A B C

17. Pattern 17 - Half Pyramid of Reversed Numbers

#include <stdio.h> int main() { int rows, i, j; printf("Enter the number of rows: "); scanf("%d", &rows);

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

Output:

Enter the number of rows: 5 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1

18. Pattern 18 - Floyd's Triangle

#include <stdio.h> int main() { int rows, i, j, num = 1; printf("Enter the number of rows: "); scanf("%d", &rows); for (i = 1; i <= rows; i++) { for (j = 1; j <= i; j++) { printf("%d ", num); num++; } printf("\n"); } return 0; }

Output:

Enter the number of rows: 4 1 2 3 4 5 6 7 8 9 10

19. Pattern 19 - Rhombus Pattern

#include <stdio.h> int main() { int n, i, j; printf("Enter the number of rows: "); scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= n - i; j++) { printf(" "); } for (j = 1; j <= n; j++) { printf("*"); } printf("\n"); } return 0; }

Output:

Enter the number of rows: 5 ***** ***** ***** ***** *****

20. Pattern 20 - Hollow Pyramid with Numbers

#include <stdio.h> int main() { int n, i, j, k = 0; printf("Enter the number of rows: "); scanf("%d", &n); for (i = 1; i <= n; i++, k = 0) { for (j = 1; j <= n - i; j++) { printf(" "); } while (k != 2 * i - 1) { if (k == 0 || k == 2 * i - 2 || i == n) printf("%d ", i); else printf(" "); k++; } printf("\n"); } return 0; }

Output:

Enter the number of rows: 5 1 2 2 3 3 4 4 5 5 5 5 5 5 5 5 5
































































































Comments

Popular posts from this blog

Video Formats

Where video editing is used

File saving and folder management