C program to print mirrored half diamond star pattern
    *
   **
  ***
 ****
*****
 ****
  ***
   **
    *

C Programming Language / Loop control in C Language

1409

Program:

/**
 * C program to print mirrored half diamond star pattern
* atnyla.com
 */

#include <stdio.h>

int main()
{
    int i, j, N;
    int star, spaces;
    
    /* Input number of columns to print from user */
    printf("Enter number of columns : ");
    scanf("%d", &N);

    
    spaces = N-1;
    star = 1;
    
    /* Iterate through rows */
    for(i=1; i<N*2; i++)
    {
        /* Print leading spaces */
        for(j=1; j<=spaces; j++)
            printf(" ");
        
        /* Print stars */
        for(j=1; j<=star; j++)
            printf("*");
        
        /* Move to next line */
        printf("\n");
        
        if(i < N) 
        {
            star++;
            spaces--;
        }
        else
        {
            star--;
            spaces++;
        }
    }

    return 0;
}

Output:

Enter number of columns : 5
    *
   **
  ***
 ****
*****
 ****
  ***
   **
    *

Explanation:

Required knowledge

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

Logic to print mirrored half diamond star pattern

 

    *
   **
  ***
 ****
*****
 ****
  ***
   **
    *

 

The above pattern is almost similar to half diamond star pattern if you remove leading spaces. Spaces as well as stars in the given pattern are printed in specific fashion. Which is stars are arranged in increasing order from 1 to Nth row. After Nth row stars are printed in decreasing order. Spaces are printed in decreasing order till Nth row. After Nth row spaces are arranged in increasing order. You can point your mouse pointer over the pattern to count total spaces.

Step by step descriptive logic to print mirrored half diamond star pattern.

  1. Input number of columns to print from user. Store it in some variable say N.
  2. To print spaces and stars, we need a variable to keep track of total columns to print each row. Declare and initialize two variables spaces=N-1 and star=1.
  3. To iterate through rows, run an outer loop from 1 to N*2-1. The loop structure should look like for(i=1; i<N*2; i++).
  4. To print spaces, run an inner loop from 1 to spaces. The loop structure should look like for(j=1; j<=spaces; j++). Inside this loop print single space.
  5. To print stars, run another inner loop from 1 to stars. The loop structure should look like for(j=1; j<=stars; j++). Inside this loop print star.
  6. After printing all columns of a row, move to next line i.e. print a new line.

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.