In our Last lesson we learned printing Square Pattern, I hope you would have done your tasks and observed the changes. Now, Lets Move to print Right Triangle Pattern. In the case of the right-angle triangle pattern, we need two nested loops: an outer loop for the rows and an inner loop for the columns. The outer loop determines the number of rows in the triangle, while the inner loop controls the number of columns within each row. By manipulating the iterations of these loops, we can create different variations of the right-angle triangle pattern.

## Code Examples:

### C++ Code:

```
#include <iostream>
int main() {
int n;
std::cout << "Enter the number of rows in the right-angle triangle pattern: ";
std::cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
std::cout << "* ";
}
std::cout << std::endl;
}
return 0;
}
```

### Python Code:

```
n = int(input("Enter the number of rows in the right-angle triangle pattern: "))
for i in range(1, n+1):
for j in range(1, i+1):
print("*", end=" ")
print()
```

### Output:

For the input `n = 5`

, the output of both codes will be:

```
*
* *
* * *
* * * *
* * * * *
```

### Explanation of the Code:

- We prompt the user to enter the number of rows in the right-angle triangle pattern and store the value in the variable
`n`

. - The outer loop, controlled by the variable
`i`

, runs from 1 to`n`

, iterating over the rows of the triangle pattern. - Inside the outer loop, the inner loop, controlled by the variable
`j`

, runs from 1 to`i`

, iterating over the columns of each row. - Within the inner loop, we print the ‘*’ symbol followed by a space using the
`cout`

statement (in C++) or the`print`

function (in Python). - After the inner loop completes, we print a newline character to move to the next row using
`cout << endl`

(in C++) or`print()`

(in Python). - This process repeats for each row until the right-angle triangle pattern is complete.

## Experiment Time

Below is the pattern, Guess what we need to replace * with, i or j? to get this Pattern.

For the input `n = 5`

, the output of both codes will be:

```
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
```

## Explanation of the This Output:

By replacing the ‘*’ symbol with the loop variable ‘i’, we print the value of ‘i’ in each cell of the right-angle triangle pattern. As the outer loop progresses, the value of ‘i’ increases, resulting in an increasing pattern. Each row will have the same value because the inner loop completes before moving to the next row.

## Customizing the Right-Angle Triangle Pattern:

To customize the number of rows in the right-angle triangle pattern. You can take the input ‘n’ from the user, similar to the previous examples. By modifying the value of ‘n’, you can increase or decrease the size of the triangle accordingly.

## Reversing the Triangle:

To reverse the right-angle triangle pattern, you can simply modify the condition in the outer loop to `i >= 1`

and decrement ‘i’ instead of incrementing it. This will create an upside-down version of the right-angle triangle.

I hope you learned from today’s lesson. In our Next Lesson we will print Pyramid Pattern.

## 1 thought on “Print Right Triangle Pattern”