C program to print rhombus or parallelogram star pattern

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

Example

Input

Enter number of rows: 5

Output

Required knowledge

Basic C programming, For loop

Must know - Program to print square star pattern

Logic to print rhombus star pattern

Before I decode the logic of this pattern, have a close look of the pattern. Place your mouse cursor on to the pattern, to count spaces. Try to decode the logic of given pattern.

If you remove trailing spaces the pattern becomes a simple square star pattern of N rows and columns. You only need to add logic of printing spaces with the existing logic of square star pattern. Now, notice again the pattern carefully. You will find that there are N - i spaces per row (where i is the current row number).

Step-by-step descriptive logic to print rhombus star pattern

  1. Read number of rows from user. Store it in some variable say rows.
  2. To iterate through rows, run an outer loop from 1 to rows. Define a loop with structure for(i=1; i<=rows; i++).
  3. To print spaces, run an inner loop from 1 to rows - i. Construct a loop with structure for(j=1; j<=rows - i; j++). Inside this loop print space.
  4. To iterate through columns for printing stars. Run another inner loop from 1 to rows. Define another loop with structure for(j=1; j<=rows; j++). Inside this loop print star(*) or any other character which you want to print as output.

Program to print rhombus star pattern

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

#include <stdio.h>

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

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

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

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

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

    return 0;
}

Output

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

Logic to print parallelogram star pattern

Logic to print parallelogram star pattern is same as of rhombus star pattern. With few modification you can code it out. Below edit you need to make in your above logic.

  1. Read rows and columns from user and store it in some variable say rows and columns.
  2. To iterate through rows, run an outer loop from 1 to rows. Define a loop with structure for(i=1; i<=rows; i++).
  3. Run an inner loop to print space from 1 to rows - i. The loop structure should be for(j=1; j<=rows - i; j++).
  4. To print stars run another inner loop after printing space. Run a loop from 1 to columns. Define a loop with structure for(j=1; j<=columns; j++).

Program to print parallelogram star pattern

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

#include <stdio.h>

int main()
{
    int i, j, rows, columns;

    // Input rows and columns from user
    printf("Enter rows: ");
    scanf("%d", &rows);
    printf("Enter columns: ");
    scanf("%d", &columns);

    // Iterate through each row
    for(i=1; i<=rows; i++)
    {
        // Print trailing spaces
        for(j=1; j<=rows - i; j++)
        {
            printf(" ");
        }

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

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

    return 0;
}

Output

Enter rows: 5
Enter columns: 10
    **********
   **********
  **********
 **********
**********

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>