Half diamond number pattern with star border program in C – 1

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

Required knowledge

Basic C programming, Loop

Logic to print the given half diamond number pattern with star border

Let’s first remove the border of the given pattern. After removing border the pattern look like.

I have already explained the logic to print the above pattern in detail in my previous post. I highly recommend you to go through the pattern before moving on to this. As this entire pattern is fully based on my previous number pattern.

Now, once you got the logic of half diamond number pattern without star border. Let’s move on to the pattern with star border. Here in this pattern we only need to add the logic to print borders. Printing star (*) as border is simple. We only need to add an extra printf(“*”); statement before and/or after every loop as needed.

Program to print the given half diamond number pattern with star border

/**
 * C program to print the half diamond number pattern with star border
 */

#include <stdio.h>

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

    printf("Enter rows: ");
    scanf("%d", &N);

    printf("*\n");
    // Print the first upper half
    for(i=1; i<=N; i++)
    {
        printf("*");
        for(j=1; j<=i; j++)
        {
            printf("%d", j);
        }

        for(j=i-1; j>=1; j--)
        {
            printf("%d", j);
        }
        printf("*");

        printf("\n");
    }

    // Print the lower half of the pattern
    for(i=N-1; i>=1; i--)
    {
        printf("*");
        for(j=1; j<=i; j++)
        {
            printf("%d", j);
        }

        for(j=i-1; j>=1; j--)
        {
            printf("%d", j);
        }
        printf("*");

        printf("\n");
    }
    printf("*");

    return 0;
}

Output

Enter rows: 5
*
*1*
*121*
*12321*
*1234321*
*123454321*
*1234321*
*12321*
*121*
*1*
*

Happy coding 😉