C program to print diamond star pattern

Write a C program to print the diamond star(*) pattern series using for loop. How to print diamond star pattern structure using for loop in C programming. Logic to print diamond star pattern series in C program.

Example

Input

Input rows: 5

Output

Required knowledge

Basic C programming, For loop

Logic to print diamond star pattern

At single glance the patter seems difficult to be printed so, to make the pattern easy I have bisected the pattern in two halves.

If you notice these two patterns are simple pyramid (with n rows) and reverse pyramid pattern (with n-1 rows). Hence, we just need to write down the codes of both pyramid and reverse pyramid star pattern one by one to get the final pattern.

Program to print diamond star pattern

/**
 * C program to print diamond star pattern 
 */

#include <stdio.h>

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

    printf("Enter N : ");
    scanf("%d", &n);
    
    // Print the upper pyramid
    for(i=1; i<=n; i++)
    {
        for(j=i; j<n; j++)
        {
            printf(" ");
        }
        for(j=1; j<=(2*i-1); j++)
        {
            printf("*");
        }
        printf("\n");
    }

    // Print the lower triangle
    for(i=n; i>=1; i--)
    {
        for(j=i; j<=n; j++)
        {
            printf(" ");
        }
        for(j=2; j<(2*i-1); j++)
        {
            printf("*");
        }
        printf("\n");
    }
    
    return 0;
}

Output

Enter N: 5
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>