*
***
*****
*******
*********
*******
*****
***
*
/**
* C program to print diamond star pattern
*/
#include <stdio.h>
int main()
{
int i, j, rows;
int stars, spaces;
printf("Enter rows to print : ");
scanf("%d", &rows);
stars = 1;
spaces = rows - 1;
/* Iterate through rows */
for(i=1; i<rows*2; i++)
{
/* Print spaces */
for(j=1; j<=spaces; j++)
printf(" ");
/* Print stars */
for(j=1; j<stars*2; j++)
printf("*");
/* Move to next line */
printf("\n");
if(i<rows)
{
spaces--;
stars++;
}
else
{
spaces++;
stars--;
}
}
return 0;
}
Enter N: 5
*
***
*****
*******
*********
*******
*****
***
*
Basic C programming, If else, For loop, Nested loop
*
***
*****
*******
*********
*******
*****
***
*
The given pattern is a combination of simple pyramid star pattern and inverted pyramid star pattern. It consist of N*2-1 rows (for this case N=5). Each row contain spaces and stars in printed in increasing and decreasing order.
Stars are printed in increasing order till Nth row. After Nth row stars are printed in decreasing order.
Spaces are printed in decreasing order till Nth row. After Nth row spaces are printed in increasing order. Point your mouse cursor over the pattern to count total spaces.
Step by step descriptive logic to print diamond star pattern.
stars=1 and spaces=N-1.rows*2-1. The loop structure should look like for(i=1; i. for(j=1; j<=spaces; j++). Inside this loop print single space.stars*2-1. The loop structure should look like for(j=1; j<=stars; j++). Inside this loop print star.First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.