C program to print rhombus or parallelogram star pattern

Previous Pattern Next Pattern
Write a C program to print rhombus star(*) pattern series of N rows using for loop. How to print rhombus star pattern series using for loop in C programming. How to print parallelogram star pattern series of M rows and N columns using for loop in C programming. Logic to print rhombus or parallelogram star pattern series in C program.

Example:
Input M: 5
Input N: 20
Output:

Required knowledge

Basic C programming, For loop

Logic to print rhombus star pattern

Before discussing the logic of this pattern I recommend you to go through square star pattern series if you aren't familiar with the basics of star pattern. Also this pattern is based upon square star pattern series.

Now talking about the pattern, the rhombus pattern consists of N rows and columns. If you neglect the trailing spaces the logic to print the pattern might look similar to the previous square star pattern. Therefore, we only need to add the logic of printing trailing spaces before the square star pattern. If you have noticed, there are N - i spaces per row (where i is the current row number). To see or count spaces per row, just hover or click your mouse pointer on to the above pattern.

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 to be printed).
  2. To print spaces, run an inner loop from 1 to N - i (where i is the current row number). Inside this loop print single blank space.
  3. To iterate through columns and to print numbers, 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 rhombus star pattern

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

#include <stdio.h>

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

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

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

        //Print stars after spaces
        for(j=1; j<=N; j++)
        {
            printf("*");
        }

        //Moves to the next line/row
        printf("\n");
    }

    return 0;
} 


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


Rhombus star pattern screenshot

Rhombus star pattern in C


Logic to print parallelogram star pattern

Logic to print parallelogram star pattern is exactly same as of rhombus star pattern. The only thing you need to change is, you need to iterate though M rows and N columns (where M is the total number of rows to be printed and N is the total number of columns per row).

Program to print parallelogram star pattern

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

#include <stdio.h>

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

    //Reads total rows and columns 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<=M - i; j++)
        {
            printf(" ");
        }

        //Print stars after spaces
        for(j=1; j<=N; j++)
        {
            printf("*");
        }

        //Moves to the next line/row
        printf("\n");
    }

    return 0;
}


Parallelogram star pattern screenshot

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