***** ***** ***** ***** *****
/**
* C program to print Rhombus star pattern series
* atnyla.com
*/
#include <stdio.h>
int main()
{
int i, j, rows;
/* Input number of rows from user */
printf("Enter rows: ");
scanf("%d", &rows);
for(i=1; i<=rows; i++)
{
/* Print trailing spaces */
for(j=1; j<=rows - i; j++)
{
printf(" ");
}
/* Print stars after spaces */
for(j=1; j<=rows; j++)
{
printf("*");
}
/* Move to the next line */
printf("\n");
}
return 0;
}
Enter rows: 5
*****
*****
*****
*****
*****
***** ***** ***** ***** *****
Before I decode the logic of this pattern, have a close look of the pattern. Place your mouse cursor on to the pattern, to count spaces. Try to decode the logic of given pattern.
If you remove trailing spaces the pattern becomes a simple square star pattern of N rows and columns. You only need to add logic of printing spaces with the existing logic of square star pattern.
The pattern consists N - i spaces per row (where i is the current row number).
Step by step descriptive logic to print rhombus star pattern
for(i=1; i<=rows; i++).rows - i. Construct a loop with structure for(j=1; j<=rows - i; j++). Inside this loop print space.for(j=1; j<=rows; j++). Inside this loop print star.First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.