C Program to print a Hollow mirrored Rhombus Star Pattern – In this article, we will discover how to print a hollow mirrored rhombus star pattern in various ways in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The ways mentioned in this piece are as follows:
- Using For Loop
- Using While Loop
- Using Do-While Loop
As we all know, a rhombus is a two-dimensional quadrilateral in the field of geometry.
A rhombus, as we all know, has equal sides and the opposite sides are parallel. The opposite angles are also equal in nature.
The diagonals of a rhombus cut each other at right angles.
A rhombus is basically a parallelogram where two adjacent sides are equal.
As you can see, we have to first enter the number of rows. Depending on that, the rhombus can be printed using the symbol “*”.
Thus, the various methods used to print a hollow mirrored rhombus star pattern are as follows:
Using For Loop
- Read the no.of rows, entered the symbol and store the values into the variables n, ch.
2) The outer for loop iterates through rows with the structure for(i=1;i<=n;i++).
3) The 1st inner for loop iterates through columns with the structure for(j=i;j>0;j–).
a) Prints space.
4) For 1st row,nth row(i=1 or n) the 2nd inner for loop iterates through columns with the structure for(j=1;j<=n;j++).
a) Prints symbol.
5) For i!=1 or i!=n(other than 1st row,last row)
The 3rd inner for loop iterates through columns with the structure for(j=1;j<=n;j++)
a) Prints symbol if j=1 or n
b) Otherwise it prints space.
6) For each iteration of the outer loop, cursor comes to next line.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
#include <stdio.h> #include <conio.h> int main() { int i,j,n; char ch; printf("Enter number of rows: "); scanf("%d%c",&n,&ch); printf("Enter the symbol: "); ch=getchar(); for(i=1;i<=n;i++) { for(j=i;j>0;j--) { printf(" "); } if(i==1 || i==n) for(j=1;j<=n;j++) { printf("%c",ch); } else { for(j=1;j<=n;j++) { if(j==1 || j==n) printf("%c",ch); else printf(" "); } } printf("\n"); } return 0; } |
Output:
1 2 3 4 5 6 7 |
Enter number of rows: 5 Enter the symbol: * ***** * * * * * * ***** |
Using While Loop
- The outer while loop iterates through rows until the condition i<=n becomes false.
2) The 1st inner while loop iterates through columns and prints space until the condition –j>0 becomes false.
3) For 1st row, nth row(i=1 or i=n)
The 2nd inner while loop iterates through columns and prints symbol until the condition j<=n becomes false.
4) For i!=1 or i!=n(other than 1st row,nth row)
The 3rd inner while loop iterates through columns until the condition j<=n becomes false.
a) Prints symbol if j=1 or j=n.
b) Otherwise prints space.
5) For each iteration of the outer while loop, the cursor comes to next line.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
#include <stdio.h> #include <conio.h> int main() { int i,j,n; char ch; printf("Enter number of rows: "); scanf("%d%c",&n); printf("Enter the symbol: "); ch=getchar(); i=1; while(i<=n) { j=i; while(--j>0) { printf(" "); } if(i==1 || i==n) { j=1; while(j <=n) { printf("%c",ch); j++; } } else { j=1; while(j<=n) { if(j==1 || j==n) printf("%c",ch); else printf(" "); j++; } } printf("\n"); i++; } return 0; } |
Output:
1 2 3 4 5 6 7 |
....Enter number of rows: 5 Enter the symbol: . ..... . . . . . . ..... |
Using Do-While Loop
- The outer do-while loop iterates through rows.
2) The 1st inner do-while loop iterates through columns.
a) Prints space.
b) j value decreased by 1.
c) Checks the condition –j>0. If the condition is true then loop iterates again.Repeats until the condition becomes false.
3) For 1st row, nth row.
The 2nd inner do-while loop iterates through columns.
a) Prints symbol if j=1 or n.
b) Otherwise prints space.
c) j value increased by 1.
d) Checks the condition ++j<=n.If the condition is true loop iterates again.Repeats until the condition ++j<=n becomes false.
4) Cursor comes to next line.
5) i value increased by 1.Checks the condition i<=n. If the condition is true then outer loop iterates again. Repeats until the condition i<=n becomes false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#include <stdio.h> #include <conio.h> int main() { int i,j,n; char ch; printf("Enter number of rows: "); scanf("%d%c",&n); printf("Enter the symbol: "); ch=getchar(); i=1; do { j=i; do { printf(" "); } while(--j>0); if(i==1 || i==n) { j=1; do { printf("%c",ch); }while(++j<=n); } else { j=1; do { if(j==1 || j==n) printf("%c",ch); else printf(" "); }while(++j<=n); } printf("\n"); ++i; } while(i<=n); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 |
Enter number of rows: 9 Enter the symbol: 0 000000000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 000000000 |