********** **** **** *** *** ** ** * * * * ** ** *** *** **** **** **********
/**
* C program to print hollow diamond star pattern
* atnyla.com
*/
#include <stdio.h>
int main()
{
int i, j, n;
printf("Enter value of n : ");
scanf("%d", &n);
// Loop to print upper half of the pattern
for(i=1; i<=n; i++)
{
for(j=i; j<=n; j++)
{
printf("*");
}
for(j=1; j<=(2*i-2); j++)
{
printf(" ");
}
for(j=i; j<=n; j++)
{
printf("*");
}
printf("\n");
}
// Loop to print lower half of the pattern
for(i=1; i<=n; i++)
{
for(j=1; j<=i; j++)
{
printf("*");
}
for(j=(2*i-2); j<(2*n-2); j++)
{
printf(" ");
}
for(j=1; j<=i; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Enter value of n : 5
**********
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
**********
Basic C programming, For loop, Nested loop
Must know - Program to print diamond star pattern
The pattern seems to be one of the complex pattern to think. To make it easier, let us bisect in two halves.
********** **** **** *** *** ** ** * * * * ** ** *** *** **** **** **********
Here in the upper part of the pattern, trailing and leading stars are inverted right triangle pattern that can be easily printed. Each row contains 2*rownumber - 2 spaces.
Moving on to the second half, if you look at the trailing and leading stars you will find that both of them are right triangle star pattern and total number of spaces per row is 2*rownumber - 2.
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.