C program to print hollow rhombus, parallelogram star pattern
*****
* *
* *
* *
*****
C Programming Language Loop control in C Language (Article) Loop control in C Language (Program)
1364Program:
/** * C program to print hollow rhombus star pattern * atnyla.com */ #include 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 and center spaces */ for(j=1; j<=rows; j++) { if(i==1 || i==rows || j==1 || j==rows) printf("*"); else printf(" "); } printf("\n"); } return 0; } Output
Output:
Enter rows: 5 ***** * * * * * * *****
Explanation:
Logic to print hollow rhombus star pattern
***** * * * * * * *****
Step by step descriptive logic to print rhombus star pattern.
- Input number of rows to print from user. Store it in a variable say rows.
- To iterate through rows, run an outer loop from 1 to rows. Define an outer loop with structure
for(i=1; i<=rows; i++)
. - To print trailing spaces, run an inner loop from 1 to
rows - i
. Run a loop with structurefor(j=1; j<=rows - i; j++)
. Inside this loop print blank space. - To print stars, run another inner loop from 1 to rows with structure
for(j=1; j<=rows; j++)
. - Inside this loop print star for first or last row, and for first or last column otherwise print spaces. Which is print stars only when
i==1
ori==rows
orj==1
orj==rows
. - After printing all columns of a row move to next line i.e. print new line.
This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.