Hi Students, let’s write another pattern program in C language! Print Diamond star pattern using C program:
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 41 42 43 44 45 46 47 48 49 |
#include <stdio.h> int main() { int limit, space, i, j; //print a string about the program printf("C Program to print diamond pattern\n"); printf("Enter the limit: "); //get the limit scanf("%d", &limit); //print the first half of the pyramid space = limit; for (i = 1; i <= limit; i++) { for (j = 1; j <= space; j++) { printf(" "); } space--; for (j = 1; j <= 2 * i - 1; j++) { printf("*"); } printf("\n"); } //print the second half of the pyramid space = 2; for (i = 1; i <= limit; i++) { for (j = 1; j <= space; j++) { printf(" "); } space++; for (j = 1; j <= 2 * (limit - i) - 1; j++) { printf("*"); } printf("\n"); } } |
Sample Output of the above program:
1 2 3 4 5 6 7 8 9 10 11 |
C Program to print diamond pattern Enter the limit: 5 * *** ***** ******* ********* ******* ***** *** * |