**Problem Statement:** Given an integer **N, **print the following pattern :

Here, N = 5.

**Examples**:

Input Format: N = 3Result: 1 1 2 1 2 3Input Format: N = 6Result: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6

**Solution**

** Disclaimer**:

*Don’t jump directly to the solution, try it out yourself first.*

**Approach**:

There are 4 general rules for solving a pattern-based question :

- We always use nested loops for printing the patterns. For the outer loop, we count the number of lines/rows and loop for them.
- Next, for the inner loop, we focus on the number of columns and somehow connect them to the rows by forming a logic such that for each row we get the required number of columns to be printed.
- We print the ‘*’ inside the inner loop.
- Observe symmetry in the pattern or check if a pattern is a combination of two or more similar patterns or not.

In this pattern, we run the outer loop for N times as we have to print N rows, and since we have to print a right-angled triangle/pyramid which must be upright, so the inner loop will run for the row number in each iteration. For eg: 1 number for row 1, 5 numbers for row 5, and so on. The only difference between this pattern and pattern 2 is that here we print **numbers** looping from 1 to the row number for each row instead of printing stars.

**Code**:

## C++ Code

```
#include <bits/stdc++.h>
using namespace std;
void pattern3(int N)
{
// This is the outer loop which will loop for the rows.
for (int i = 1; i <= N; i++)
{
// This is the inner loop which loops for the columns
// no. of columns = row number for each line here.
// Here, we print numbers from 1 to the row number
// instead of stars in each row.
for (int j = 1; j <=i; j++)
{
cout <<j<<" ";
}
// As soon as numbers for each iteration are printed, we move to the
// next row and give a line break otherwise all numbers
// would get printed in 1 line.
cout << endl;
}
}
int main()
{
// Here, we have taken the value of N as 5.
// We can also take input from the user.
int N = 5;
pattern3(N);
return 0;
}
```

**Output **

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

## Java Code

```
class Main {
static void pattern3(int N)
{
// This is the outer loop which will loop for the rows.
for (int i = 1; i <= N; i++)
{
// This is the inner loop which loops for the columns
// no. of columns = row number for each line here.
// Here, we print numbers from 1 to the row number
// instead of stars in each row.
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
// As soon as numbers for each iteration are printed, we move to the
// next row and give a line break otherwise all numbers
// would get printed in 1 line.
System.out.println();
}
}
public static void main(String[] args) {
// Here, we have taken the value of N as 5.
// We can also take input from the user.
int N = 5;
pattern3(N);
}
}
```

**Output **

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

Special thanks toplease check out this article.Priyanshi Goelfor contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam,If you want to suggest any improvement/correction in this article please mail us at [email protected]