In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a character pattern in C using for loop. So you may go through the following topic in C.
Pattern 1: Diamond pattern in c using alphabets
Enter the no. of rows: 8
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFGHIJK
ABCDEFGHIJKLM
ABCDEFGHIJKLMNO
ABCDEFGHIJKLM
ABCDEFGHIJK
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A
Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <stdio.h> void main() { int rows, i, j, k; printf("Enter the no. of rows: "); scanf("%d", &rows); // ASCII value int alphabet = 64; for (i = 1; i <= rows; i++) { for (j = 1; j <= rows - i; j++) printf(" "); for (k = 1; k <= i *2 - 1; k++) printf("%c", alphabet + k); printf("\n"); } for (i = rows - 1; i > 0; i--) { for (j = 1; j <= rows - i; j++) printf(" "); for (k = 1; k <= i *2 - 1; k++) printf("%c", alphabet + k); printf("\n"); } } |
Pattern 2: Diamond pattern in c using alphabets
Enter the no. of rows: 8
A
A B
A B C
A B C D
A B C D E
A B C D E F
A B C D E F G
A B C D E F G H
A B C D E F G
A B C D E F
A B C D E
A B C D
A B C
A B
A
Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <stdio.h> void main() { int rows, i, j, k; printf("Enter the no. of rows: "); scanf("%d", &rows); // ASCII value int alphabet = 65; for (i = 0; i <= rows - 1; i++) { for (j = rows - 1; j >= i; j--) printf(" "); for (k = 0; k <= i; k++) printf("%c ", alphabet + k); printf("\n");; } for (i = 0; i <= rows - 1; i++) { for (j = -1; j <= i; j++) printf(" "); for (k = 0; k <= rows - i - 2; k++) printf("%c ", alphabet + k); printf("\n");; } } |
Pattern 3: Diamond pattern in c using alphabets
Enter the no. of rows: 8
A
BAB
CBABC
DCBABCD
EDCBABCDE
FEDCBABCDEF
GFEDCBABCDEFG
HGFEDCBABCDEFGH
GFEDCBABCDEFG
FEDCBABCDEF
EDCBABCDE
DCBABCD
CBABC
BAB
A
Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include <stdio.h> void main() { int rows, i, j, k, l; printf("Enter the no. of rows: "); scanf("%d", &rows); // ASCII value int alphabet = 64; for (i = 1; i <= rows; i++) { for (j = 1; j <= rows - i; j++) printf(" "); for (k = i; k >= 1; k--) printf("%c", alphabet + k); for (l = 2; l <= i; l++) printf("%c", alphabet + l); printf("\n"); } for (i = rows - 1; i > 0; i--) { for (j = 1; j <= rows - i; j++) printf(" "); for (k = i; k >= 1; k--) printf("%c", alphabet + k); for (l = 2; l <= i; l++) printf("%c", alphabet + l); printf("\n"); } } |