C program to print hollow rhombus, parallelogram star pattern

Write a C program to print hollow rhombus star series pattern using for loop. How to print hollow rhombus or parallelogram star pattern in C programming. Logic to print empty rhombus or parallelogram star pattern series in C programming.

Example

Input

Input number of rows: 5

Output

Required knowledge

Basic C programming, If else, For loop, Nested loop

Must know –

Logic to print hollow rhombus star pattern

Step by step descriptive logic to print rhombus star pattern.

  1. Input number of rows to print from user. Store it in a variable say rows.
  2. To iterate through rows, run an outer loop from 1 to rows. Define an outer loop with structure for(i=1; i<=rows; i++).
  3. To print trailing spaces, run an inner loop from 1 to rows - i. Run a loop with structure for(j=1; j<=rows - i; j++). Inside this loop print blank space.
  4. To print stars, run another inner loop from 1 to rows with structure for(j=1; j<=rows; j++).
  5. Inside this loop print star for first or last row, and for first or last column otherwise print spaces. Which is print stars only when i==1 or i==rows or j==1 or j==rows.
  6. After printing all columns of a row move to next line i.e. print new line.

Program to print hollow rhombus star pattern

/**
 * C program to print hollow rhombus star pattern
 */

#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 and center spaces */
        for(j=1; j<=rows; j++)
        {
            if(i==1 || i==rows || j==1 || j==rows)
                printf("*");
            else
                printf(" ");
        }

        printf("\n");
    }

    return 0;
}

Output

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

Hurray!!! we printed a hollow rhombus star pattern. What’s next now? You can modify the above logic to work for parallelogram. Let us write C code to print hollow parallelogram.

Program to print hollow parallelogram star pattern

/**
 * C program to print hollow parallelogram star pattern
 */

#include <stdio.h>

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

    /* Input number of row and column */
    printf("Enter rows : ");
    scanf("%d", &rows);
    printf("Enter columns: ");
    scanf("%d", &columns);

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

        /* Print stars and center spaces */
        for(j=1; j<=columns; j++)
        {
            if(i==1 || i==rows || j==1 || j==columns)
                printf("*");
            else
                printf(" ");
        }

        printf("\n");
    }

    return 0;
}

Output

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

Happy coding 😉