Hi everyone in this article we are writing a C Program to print a diamond pattern. In this program will take input number to as the rows of the pattern that we have to print so that the diamond can be large as you want want and small as you want it to be.
Prefer to watch? Here's the video version of this post.
So now let's start the program.
code:
#include <stdio.h>#include <conio.h>int main(){int n, c, k, space = 1;printf("Enter number of rows \n");scanf("%d", &n);space = n - 1;for(k = 1; k <= n; k++){for(c = 1; c <= space; c++)printf(" ");space--;for(c = 1; c <= 2 * k - 1; c++)printf("*");printf("\n");}space = 1;for(k = 1; k <= n - 1; k++){for(c = 1; c <= space; c++)printf(" ");space++;for(c = 1; c <= 2 * (n - k); c++)printf("*");printf("\n");}getch();return 0;}
output:
When the executable asks for the input if the input is
number of rows = 5
then output will be
| output - diamond pattern |