C program to print mirrored half diamond star pattern

C program to print mirrored half diamond star pattern

In this article, I will show you, How to write a C program to print mirrored half diamond star patterns. How to print mirrored half diamond star pattern in C programming. Here, one thing is important to know that the columns of the mirrored half diamond.

The above pattern is almost similar to half diamond star pattern if you remove leading spaces. If you will see the design pattern, you will find that star is increasing 1 to Nth row and once it reaches to Nth row, it is decreasing till 1.

Logic to C program to print mirrored half diamond star pattern:

  • Enter the column value for the mirrored half diamond.
  • To print spaces and stars, I am using two variables space and star. I have initialized space with (column – 1) and star with 1.
  • Here I have used three loops one is the outer loop to change the line and increment the star and space variable. The other loops are used to print the stars and spaces.
  • The outer loop iterates (column*2) times. The inner loop iterates star and spacetimes to print the star and space.

 

#include <stdio.h>
int main()
{
    int x = 0,y = 0;
    unsigned int coloumn = 0;
    unsigned int star = 0;
    unsigned int space = 0;
    printf("Enter the number of coloumn = ");
    scanf("%u",&coloumn);
    space = (coloumn - 1);
    for(x=1; x < (coloumn*2) ; ++x)
    {
        // Print spaces
        for(y=0; y < space; ++y)
        {
            printf(" ");
        }
        // Print star
        for(y=0; y < star; ++y)
        {
            printf("*");
        }
        // Print new line
        printf("\n");
        if(x<coloumn)
        {
            ++star;
            --space;
        }
        else
        {
            ++space;
            --star;
        }
    }
    return 0;
}

 

Output:

print mirrored half diamond star pattern

Leave a Reply

Your email address will not be published. Required fields are marked *