C Program to print a Mirrored Rhombus Star Patter – In this particular article, we will brief in on the various methods used to print a Mirrored Rhombus Star Pattern in C Programming.
The ways in which the Mirrored Rhombus Star Pattern is printed in this article are as follows:
- Using For Loop
- Using While Loop
- Using Do-While Loop
As we all know, a Rhombus is a quadrilateral which is bound in nature and has sides of equal length. The opposite sides of a rhombus are parallel to each other.
The opposite angles of a rhombus are also equal in nature. Due to all these characteristics, the diagonals of a rhombus intersect each other at right angles.
In this piece, students have to print a star pattern. Star patterns are basically the introductory level of teaching logic and how to use it in the art of programming.
The Mirrored Rhombus Star Pattern looks like this:
As you can see, this is how a Mirrored Rhombus Star Pattern should look like. To print this in C Programming, you will have to check the whole article as you have no other choice right now after clicking this.
Also, check out the sample outputs and sample programs. At the end of the post, we do list out the more C programs and also online execution tool for you. Do check it out.
Using For Loop
1) Read the no.of rows, store the value into the variable n, read the entered symbol and store it in the variable ch.
2) The outer for loop iterates through rows from i=1 to n with the structure for(i=1;i<=n;i++).
3) The 1st inner for loop iterates through columns from j=i to 1 with the structure for(j=i;j>0;j–).
a) Prints space.
4) For i=1 or n(1st row or nth row)
a) The 2nd inner for loop iterates through columns with the structure for(j=1;j<=n;j++) and prints symbol.
5) For i!=1 or i!=n(other than 1st row,nth 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 j=n
b) Otherwise it prints space.
6) The cursor comes to next line for each iteration.
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 8 |
Enter number of rows: 6 Enter the symbol: . ...... ...... ...... ...... ...... ...... |
Using While Loop
1) 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 i=1 or n(1st row,nth row)
The 3rd inner while loop iterates through columns until the condition j<=n
a) Prints symbol.
4) for i!=1 or i!=n(for other than 1st row,nth row)
The 4th inner while loop iterates through columns until the condition j<=n becomes false.
a) Prints symbol if j=1 or j=n
b) Otherwise it print space.
5) For each iteration of the outer while-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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#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; } |
1 2 3 4 5 6 7 |
Enter number of rows: 5 Enter the symbol: . ..... ..... ..... ..... ..... |
Using Do-While Loop
1) For i=1 the outer do-while loop iterates the loop one time through rows.
2) a) For j=i the 1st inner do-while loop iterates through columns and print one space.
b) j value decreased by 1.
c) Checks the condition –j>0. If condition true loop iterates again.Repeats until the condition becomes false.
3) a) For j=1 the 2nd inner do-while loop iterates through columns and prints one character.
b) j value increased by 1.
c) Checks the condition ++j<=n.If the condition is true loop iterates again. Repeat until the condition becomes false.
4) Cursor comes to next line.
5) a) i value increased by 1.
b) Checks the condition i<=n.If the condition is true then outer do-while loop iterate again.Repeats until the condition 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 |
#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); j=1; do { printf("%c",ch); }while(++j<=n); printf("\n"); i++; } while(i<=n); return 0; } |
1 2 3 4 5 6 7 |
Enter number of rows: 5 Enter the symbol: 5 55555 55555 55555 55555 55555 |