Example:
Input rows: 5
Input columns: 20
Output:
Required knowledge
Basic C programming, If else, For loopLogic to print hollow mirrored rhombus star pattern
Logic to print hollow mirrored rhombus star pattern is very much similar to the hollow rhombus star pattern.The above pattern contains N rows and columns (where N is the total number of rows to be printed). If you can notice there are exactly i - 1 spaces each row (where i is the current row number). To see or count trailing spaces per row you can hover or click on to the above pattern. Also the stars gets printed for first or last row or for first or last column otherwise blank space gets printed.
Step-by-step descriptive logic:
- To iterate though rows, run an outer loop from 1 to N.
- To print spaces, run an inner loop from 1 to i - 1 (where i is the current row number). Inside this loop print single blank space.
- To iterate through columns and to print stars, run another inner loop from 1 to N. Inside this loop print star(*) or any other character which you want to get printed, only if it is first or last row or it is first or last column otherwise print space i.e. print stars only if i==1 OR i==N OR j==1 OR j==N (where j is the current column number).
Program to print hollow mirrored rhombus star pattern
/** * C program to print hollow mirrored rhombus star pattern series */ #include <stdio.h> int main() { int i, j, N; //Reads number of rows from user printf("Enter number of rows: "); scanf("%d", &N); for(i=1; i<=N; i++) { //Prints trailing spaces for(j=1; j<i; j++) { printf(" "); } //Prints the hollow square for(j=1; j<=N; j++) { if(i==1 || i==N || j==1|| j==N) printf("*"); else printf(" "); } printf("\n"); } return 0; }
Output
Enter the value of n: 5
***** * * * * * * *****
Hollow mirrored rhombus star pattern screenshot
Logic to print hollow mirrored parallelogram star pattern
Logic to print the given pattern is same as hollow mirrored rhombus star pattern. The only thing we need to change here is, we need to iterate through M rows and N columns (where M is the total rows to be printed and N is the total columns to be printed). Rest of the things will remain same here.Program to print hollow mirrored parallelogram star pattern
/** * C program to print hollow mirrored parallelogram star pattern series */ #include <stdio.h> int main() { int i, j, M, N; //Reads number of rows from user printf("Enter rows: "); scanf("%d", &M); printf("Enter columns: "); scanf("%d", &N); for(i=1; i<=M; i++) { //Prints trailing spaces for(j=1; j<i; j++) { printf(" "); } //Prints the hollow square for(j=1; j<=N; j++) { if(i==1 || i==M || j==1|| j==N) printf("*"); else printf(" "); } printf("\n"); } return 0; }
Hollow mirrored parallelogram star pattern screenshot
Happy coding ;)