C program to print half diamond star pattern

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

Example

Input

Input rows: 5

Output

Required knowledge

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

Read more - Program to print mirrored half diamond star pattern

Logic to print half diamond star pattern

The above pattern consist of N * 2 - 1rows. For each row columns are in increasing order till Nth row. After Nth row columns are printed in descending order.

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

  1. Input number of columns to print from user. Store it in a variable say N.
  2. Declare a variable as loop counter for each column, say columns = 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 iterate through columns, run an inner loop from 1 to columns. The loop structure should look like for(j=1; j<=columns; j++). Inside this loop print star.
  5. After printing all columns of a row, move to next line.
  6. After inner loop check if(i <= N) then increment columns otherwise decrement by 1.

Program to print half diamond star pattern

/**
 * C program to print half diamond star pattern series.
 */

#include<stdio.h>

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

    /* Input number of columns from user */
    printf("Enter number of columns:");
    scanf("%d",&N);

    columns=1;

    for(i=1;i<N*2;i++)
    {
        for(j=1; j<=columns; j++)
        {
            printf("*");
        }

        if(i < N)
        {
            /* Increment number of columns per row for upper part */
            columns++;
        }
        else
        {
            /* Decrement number of columns per row for lower part */
            columns--;
        }

        /* Move to next line */
        printf("\n");
    }

    return 0;
}

Output

Enter number of columns: 5
*
**
***
****
*****
****
***
**
*
Happy coding ;)

About Pankaj

Pankaj Prakash is the founder, editor and blogger at Codeforwin. He loves to learn new techs and write programming articles especially for beginners. He works at Vasudhaika Software Sols as a Software Design Engineer and manages Codeforwin. In short Pankaj is Web developer, Blogger, Learner, Tech and Music lover.

Follow on: Facebook | Twitter | Google | or

Comments and discussion
Have a doubt, write here. I will help my best.
Before commenting you must escape your source code before commenting. Paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>