C program to print hollow rhombus, parallelogram star pattern

Previous Pattern Next Pattern
Write a C program to print hollow rhombus star(*) series pattern using for loop. How to print hollow rhombus or parallelogram star pattern series using for loop and if else in C programming. Logic to print hollow rhombus or parallelogram star pattern series using loop in C program.

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

Required knowledge

Basic C programming, If else, For loop

Logic to print hollow rhombus star pattern

Before I get more into this star pattern I assume that you are familiar with the rhombus star pattern. If not then first learn the logic of rhombus star pattern as it will help you to formulate the logic of hollow rhombus star pattern.

The pattern consists of N rows and columns (where N is the total number of rows to be printed). There are N - i spaces each row (where i is the current row number). To see or count the number of spaces each row you can hover or click on to the above pattern. If you have noticed, the star are printed only if it is first or last row, or for first of last column otherwise space gets printed.

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 trailing 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 print stars, run another inner loop from 1 to N.
    Inside this loop print star if it is first or last row, or it is first or last column otherwise print spaces i.e. print stars only when i==1 OR i==N OR j==1 OR j==N (where j is the current column number).


Program to print hollow rhombus star pattern

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

#include <stdio.h>

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

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

        printf("\n");
    }

    return 0;
} 


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


Rhombus star pattern screenshot

Hollow rhombus star pattern in C


Logic to print hollow parallelogram star pattern

Logic to print hollow parallelogram is almost same as of hollow rhombus star pattern. What you need to change here is you 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 logic will be same as of hollow rhombus star pattern.

Program to print hollow parallelogram star pattern

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

#include <stdio.h>

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

    //Reads the number of rows and columns
    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 and center spaces
        for(j=1; j<=N; j++)
        {
            if(i==1 || i==M || j==1 || j==N)
                printf("*");
            else
                printf(" ");
        }

        printf("\n");
    }

    return 0;
}


Hollow parallelogram star pattern

Hollow 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