In this article we are going to do the C++ program in which we are going to print the pattern, Basically, the pattern looks like a right-angle triangle. so, if you observe the pattern carefully you will find that as soon as the row is increasing the column is decreasing, here in this article before moving toward the program first we will see some examples with some input and output.
C++ program to print pattern (54321 5432 543 54 5)
Input-Enter range of rows:5
Output-5 4 3 2 1
5 4 3 2
5 4 3
Input-Enter range of rows:7
Output-7 6 5 4 3 2 1
7 6 5 4 3 2
7 6 5 4 3
7 6 5 4
7 6 5
Here we need to apply the for loop, usually, here we are going to apply two for loops, one for the row and another for the column. So to print rows here we have applied the condition in for loop, Initially, we kept row =1 then we applied the condition that is row<=num and then we incremented the row.
If you observe the pattern you will find that columns are decreasing then here you will apply the condition for the column firstly we have initially kept col=num then we applied the condition col>=row till this condition the loop is going to iterate and last, we will decrement the column.
C++ program to print the pattern
using namespace std;
int main ()
cout<<“Enter range of rows:”;
Enter a range of rows:4
4 3 2 1
4 3 2
- First of all, we have to declare variables to store the value of rows, columns, and range of rows.
- Then we have taken a range of rows as user input.
- Then we started our outer for loop to print rows.
- Then we started our inner for loop inside the outer for loop to print columns.
- Then we printed the output of the program in the inner loop.
- In the end, we applied the newline ‘n’character in the outer loop to print our output in the newline after every iteration of the loop.
In this article we have seen the program in which we have printed the pattern, basically, the pattern is very simple and easy. Once you understand the logic of the program then you can easily explore it!