C program to print hollow mirrored rhombus, parallelogram star 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

Input rows: 5

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;
 
    // 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 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 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;

    // 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 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>