C program to print mirrored rhombus, parallelogram star pattern

Previous Pattern Next Pattern
Write a C program to print inverted/mirrored rhombus star pattern series using for loop. How to print inverted/mirrored rhombus or parallelogram star pattern of N rows using for loop in C programming. Logic to print mirrored rhombus or parallelogram star pattern series using loop in C program.

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

Required knowledge

Basic C programming, For loop

Logic to print mirrored rhombus star pattern

Before moving on to this pattern you might want to learn logic of printing rhombus star pattern as it is similar to mirrored rhombus star pattern.

Talking about this pattern, it contains N rows and each row contains exactly N columns (where N is the total rows to be printed). If you have noticed there are i - 1 spaces per row (where i is the current row number). You can hover or click on to the above pattern to see or count total spaces per row.

Step-by-step descriptive logic:
  1. To iterate through rows, run an outer loop from 1 to N (where N is the total number of rows).
  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 though columns and 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 as output.


Program to print mirrored rhombus star pattern

/*
 * C program to print mirrored Rhombus star pattern series
 */

#include <stdio.h>

int main()
{
    int i, j, N;

    //Reads number of rows from user
    printf("Enter rows: ");
    scanf("%d", &N);


    for(i=1; i<=N; i++)
    {
        //Prints trailing spaces 
        for(j=1; j<i; j++)
        {
            printf(" ");
        }

        for(j=1; j<=N; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
} 


Output
Enter rows: 5
*****
 *****
  *****
   *****
    *****


Mirrored rhombus star pattern screenshot

Mirrored rhombus star pattern in C


Logic to print mirrored parallelogram star pattern

Entire logic to print mirrored parallelogram star pattern will be same as of mirrored rhombus star pattern which we just printed. The only change we need to do with this pattern is we need to iterate though M rows and N columns (where M is the total number of rows to be printed and N is total number of columns to be printed). Rest of the things would be same as of first pattern we did.

Program to print mirrored parallelogram star pattern

/*
 * C program to print mirrored parallelogram star pattern series
 */

#include <stdio.h>

int main()
{
    int i, j, M, N;

    //Reads number of rows and columns
    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(" ");
        }

        for(j=1; j<=N; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
}


Mirrored parallelogram star pattern screenshot

Mirrored parallelogram star pattern in C


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