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 using for loop and if else in C programming. Logic to print empty rhombus or parallelogram star pattern series using loop in C program.

Example

Input

Input number of rows: 5

Output

Required knowledge

Basic C programming, If else, For loop

Programming knowledge required for this program.

Logic to print hollow rhombus star pattern

I have already talked about printing hollow square pattern. For this you just need to modify the logic of printing rhombus star pattern. Here you have to print stars only for first or last row and column.

Let us look in to step by step descriptive logic to print rhombus star pattern.

  1. Read number of rows to be printed from user. Store it in some 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 iterate print stars, run another inner loop from 1 to rows. Run another inner loop 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.

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!!! I printed a hollow rhombus. What next now? You can easily 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;

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

Recommended posts

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
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

No comments:

Post a Comment