C program to print hollow mirrored rhombus, parallelogram star pattern

Previous Pattern Next Pattern
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 programming. Logic to print hollow inverted rhombus or parallelogram star pattern using loop in C program.

Example:
Input rows: 5
Input columns: 20
Output:

Required knowledge

Basic C programming, If else, For loop

Logic 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:
  1. To iterate though rows, run an outer loop from 1 to N.
  2. 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.
  3. 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

Hollow mirrored rhombus star pattern in C


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

Hollow mirrored parallelogram star pattern


Happy coding ;)


Previous Pattern Next Pattern
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment