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

## Required knowledge

## Logic to print X number pattern

Before you move on to this number pattern I highly recommend you to practice some basic number patterns.

If you are Codeforwin lover. You might have already noticed that the logic to print the pattern is exactly similar to the X star pattern.

Lets move on to the logic to print the given pattern.

To make things little easier divide the pattern in two parts.

We will print both of these parts separately. We will use separate outer for loops for both the parts. Logic to print the first part of the pattern.

- The pattern consists of total N rows (where N is the total number of rows * 2 - 1). Hence the first outer loop formation to iterate through rows will be for(i=1; i<=N; i++)
- Now notice each row in the first part of the pattern. First spaces gets printed then an integer then some more spaces finally the integer. Hence we will use two inner loops to print spaces. You can hover on the above pattern to count the total number of spaces gets printed.
- Notice the trailing spaces in the first part of pattern. You might have noticed that for each row total number of trailing spaces is i - 1 (Where i is the current row number). Hence to iterate through the trailing spaces the loop formation will be for(j=1; j<i; j++). Inside this loop print a single space.
- After printing the trailing space an integer which is the current row number gets printed. Hence print the current value of i.
- Now the center spaces. Center spaces are arranged in tricky fashion. Each row contains exactly (N - i) * 2 - 1 spaces. Hence the second inner loop formation to iterate through spaces is for(j=1; j<= (N - i) * 2 - 1; j++). Inside this loop print single spaces.
- After the above loop print the value of i.

You are done with the first part of the pattern. Lets now see the logic to print the second part of the pattern. For printing second part we will use another outer loop.

- The second part of the pattern consists of N - 1 rows. Hence the outer loop formation to iterate through rows is for(i=N-1; i>=1; i--). Now, I have used a descending order loop, since the numbers printed are in descending order.
- Like first part of the pattern. Here also first trailing spaces gets printed then an integer then the central spaces and finally the same integer gets printed.
- Have a close look to the trailing spaces. Each row contains exactly i - 1 spaces i.e. the first row contains 4 - 1 => 3 spaces, second contains 3 - 1 => 2 spaces and so on. Hence the first inner loop formation will be for(j=1; j<i; j++). Inside this loop print the single space.
- After printing tailing spaces print the current value of i.
- Now notice the central spaces carefully. Each row contains exactly (N - i) * 2 - 1 central spaces. Hence the loop formation for central spaces will be for(j=1; j<=((N - i) * 2 - 1); j++). Inside this loop print single space.
- Again after central loop print the current value of i.

Finally you are done with the logic section. Embed the logic of each part of the pattern in a program. Below is the program to print the given pattern as a whole.

## Program to print X number pattern

```
/**
* C program to print X number pattern
*/
#include <stdio.h>
int main()
{
int i, j, N;
printf("Enter N: ");
scanf("%d", &N);
// First part of the pattern
for(i=1; i<=N; i++)
{
// Print trailing spaces
for(j=1; j<i; j++)
{
printf(" ");
}
printf("%d", i);
// Print central spacces
for(j=1; j<=((N - i) * 2 - 1); j++)
{
printf(" ");
}
// Don't print for last row
if(i != N)
printf("%d", i);
// Moves on to the next row
printf("\n");
}
// Second part of the pattern
for(i=N-1; i>=1; i--)
{
// Print trailing spaces
for(j=1; j<i; j++)
{
printf(" ");
}
printf("%d", i);
// Print central spaces
for(j=1; j<=((N - i ) * 2 - 1); j++)
{
printf(" ");
}
printf("%d", i);
// Move on to the next line
printf("\n");
}
return 0;
}
```

Output

Enter N: 5 1 1 2 2 3 3 4 4 5 4 4 3 3 2 2 1 1

Happy coding 😉

<pre><code> ----Your Source Code---- </code></pre>