- 1
How can I write a code In C which produce a diamond pattern
Like this * *** ***** ****** ***** *** *
3 Respuestas
0
Look for a pattern
Skip 3 spaces print 1
Skip 2 spaces print 3
Skip 1 space print 5
6
Skip 1 space print 5
Skip 2 spaces print 3
Skip 3 spaces print 1
0
#include <stdio.h>
int main(void) {
int n;
printf("Enter number of rows: \n");
scanf("%d",&n);
int spaces=n-1;
int stars=1;
for(int i=1;i<=n;i++) {
for(int j=1;j<=spaces;j++) {
printf(" ");
}
for(int k=1;k<=stars;k++) {
printf("*");
}
if(spaces>i) {
spaces=spaces-1;
stars=stars+2;
}
if(spaces<i) {
spaces=spaces+1;
stars=stars-2;
}
printf("\n");
}
return 0;
}
0
Here is my solution to print star diamond pattern. You need to print it in two steps. First upper triangle and then lower triangle.
https://code.sololearn.com/ciNIECvqDGCt
Here is the original solution of this pattern program https://www.techcrashcourse.com/2016/01/print-diamond-star-pattern-in-c.html .
Hope it helps.