Example:
Input N: 5
Required knowledge
Basic C programming, LoopLogic 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 lover of Codeforwin, 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.
- 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++).
- 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
- 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.
- 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.
Moving on to the second lower half pattern. Logic to print the given second lower half pattern is explained below.
- 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.
- 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.
- 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<=i; j++). Inside this loop print the current value of j. This part is also explained separately in my previous post.
- 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.
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
1
121
12321
1234321
123454321
1234321
12321
121
1
Screenshot of half diamond number pattern
Happy coding ;)