Write a C program to print hollow mirrored rhombus star pattern series of N rows using for loop. How to print hollow mirrored rhombus or parallelogram star pattern series in C program. Logic to print hollow inverted rhombus or parallelogram star pattern using loop in C programming.
Required knowledge
Basic C programming, If else, For loop, Nested loop
Logic to print hollow mirrored rhombus star pattern
Read more – Program to print hollow rhombus star pattern.
The above pattern contains N rows and columns (where N is total rows to print). If you can notice there are exactly i - 1
spaces each row (where i is current row number). To see or count trailing spaces per row you can hover or click on to the above pattern. Also the stars are printed only for first or last row or for first or last column otherwise blank space is printed.
Step by step descriptive logic to print hollow mirrored rhombus star pattern.
- Input number of rows to print from user. Store it in a variable say N.
- To iterate though rows, run an outer loop from 1 to N. The loop structure should look like
for(i=1; i<=N; i++)
. - To print space, run an inner loop from 1 to
i - 1
with loop structurefor(j=1; j<i; j++)
. Inside this loop print single blank space. - To print star, run another inner loop from 1 to N. The loop structure should look like
for(j=1; j<=N; j++)
. Inside this loop print stars only ifi==1
ori==N
orj==1
orj==N
. - After printing all columns of a row, move to next line i.e. print new line.
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;
/* Input number of rows from user */
printf("Enter number of rows: ");
scanf("%d", &N);
for(i=1; i<=N; i++)
{
/* Print trailing spaces */
for(j=1; j<i; j++)
{
printf(" ");
}
/* Print hollow rhombus */
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 ***** * * * * * * *****
Logic to print hollow mirrored parallelogram star pattern
Logic to print above 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 total rows to print and N is total columns to print).
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;
/* Input number of rows from user */
printf("Enter rows: ");
scanf("%d", &M);
printf("Enter columns: ");
scanf("%d", &N);
for(i=1; i<=M; i++)
{
/* Print trailing spaces */
for(j=1; j<i; j++)
{
printf(" ");
}
/* Print hollow parallelogram */
for(j=1; j<=N; j++)
{
if(i==1 || i==M || j==1|| j==N)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Happy coding 😉
Recommended posts
- Star patterns programming exercises index.
- Number pattern programming exercises index.
- Loop programming exercises index.
- Recommended patterns –
***** ***** ***** ***** *****
***** * * * * * * *****
***** ***** ***** ***** *****
***** * * * * * * *****
***** ***** ***** ***** *****