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

## Required knowledge

Basic C programming, If else, Loop

## Logic to print the given number pattern

Logic to this pattern can be little tricky on first look. The pattern contains N rows (where N is the total number of rows to be printed). There are exactly i columns per row (where i is the current row number). Now, when you look to the pattern carefully you will find that the pattern contains odd and even columns.

Read more - Program to check even number

When the row number is odd then the columns are odd, starting from first odd number and when it is even then the columns are even, starting from the first even number. For printing the numbers we will use an extra variable say k that will keep track of next number to be printed.

Step-by-step-descriptive logic:

- To iterate through rows, run an outer loop from 1 to N.
- Inside this loop initialize a variable k = 1 if the current row is odd otherwise k = 2.
- To iterate through columns, run an inner loop from 1 to i (where i is current row number).

Inside this loop print the value of k and increment k = k + 2 to get the next even or odd number.

## Program to print the given number pattern

```
/**
* C program to print number pattern
*/
#include <stdio.h>
int main()
{
int i, j, k, N;
printf("Enter N: ");
scanf("%d", &N);
for(i=1; i<=N; i++)
{
// Checking even or odd
if(i & 1)
k = 1;
else
k = 2;
// Logic to print numbers
for(j=1; j<=i; j++, k+=2)
{
printf("%d", k);
}
printf("\n");
}
return 0;
}
```

Output

Enter N: 5 1 24 135 2468 13579

Happy coding 😉

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