Half diamond number pattern program in C – 3

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

Required knowledge

Basic C programming, Loop

Logic to print the given half diamond number pattern

Getting the logic of this number pattern may seem difficult. Therefore, I recommend you to go through some of my previous posts to learn some basic number patterns. Once you get familiar with some of the number pattern read below to get the logic of given half diamond number pattern.

Without wasting time let’s get on to the logic to print the pattern. To make things easier let’s divide the given half diamond shape in half. Where the upper and lower part is shown below.

If you are a Codeforwin lover, then you have noticed that I have already explained the first upper half of the pattern here. If you haven’t gone through, read the logic to print both of the pattern below.

First of all to get the resultant pattern, we need to print both the parts separately in two separate outer loop. Let’s first learn the logic to print the first upper part of the pattern.

  1. The given pattern consists of total N rows. Hence the outer loop formation to iterate through rows will be for(i=0; i<=N; i++).
  2. Now, when you look carefully at the series in which each row gets printed. You will find that it contains two series.

    We will print these two pattern in two separate inner loops. These patterns are also explained separately in my two earlier posts

    Read more –

    1. The first part contains total i number of columns (where i is the current row number). Hence the first inner loop formation will be for(j=1; j<=i; j++). Inside this loop print the current value of j.
    2. The second part contains total i-1 number of columns each row. Hence the second inner loop formation will be for(j=i; j>=1; j–). Inside this loop print the current value of j.

    And you are done with the first upper half of the half diamond number pattern.

Moving on to the second lower half pattern. Logic to print the given second lower half pattern is explained below.

  1. The given pattern consists of total N – 1 number of rows. Hence the outer loop formation to iterate through rows will be for(i=N-1; i>=1; i–). I have used a decreasing order loop as the pattern is in decreasing order.
  2. Now, as the upper part of the pattern. This can also be divided again in two parts.

    We need two separate inner loops to print these.

    1. The first part contains total i columns (where i is the current row number). Hence the first inner loop formation will be for(j=1; j&lt=i; j++). Inside this loop print the current value of j. This part is also explained separately in my previous post.
    2. The second part contains total i – 1 columns. Hence the second inner loop formation will be for(j=i; j>=i; j–). Inside this loop print the current value of j. This pattern is also explained in detail in my previous number pattern post.

And we are done with the logic section. We need to combine all that in single program.

Program to print the given half diamond number pattern

/**
 * C program to print half diamond number pattern series
 */

#include <stdio.h>

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

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

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

        printf("\n");
    }

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

        printf("\n");
    }

    return 0;
}

Output

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

Happy coding 😉