In this article we are going to learn about the Cpp program in which we are going to print an pattern, Basically, the pattern is very simple and easy. If you observe the pattern carefully you will find that pattern is a triangular shape and every element in the row is similar. now we will see the input and output then we will move toward the program.
Cpp pattern program to print 5 44 333 2222 11111
Examples-
Example-1: Input-Enter range of rows:7
Output-7
6 6
5 5 5
4 4 4 4
3 3 3 3 3
2 2 2 2 2 2
1 1 1 1 1 1 1
Example-2: Input-Enter range of rows:3
Output-3
2 2
1 1 1
Let’s discuss the logic of the program-
In this program, we do have not much to do we have to only just take input from the user and start for loop twice ( Outer for the loop and inner for the loop) outer for the loop to print rows, and inner for the loop to print columns respectively.
Now in the first loop, we started the loop from a number that is user input up to 1 by decrementing the value of a number by 1.In inner loop we were printing columns we will start the inner for loop with a number that is input from the user and decrement up to 1 in the inner for loop we will print the output of the program after that in the outer for loop we will apply the new line character ‘n’ after every iteration of the loop we have to be in a new line from our pattern will get made. This is how we will write a program.
Program –
#include<iostream>
using namespace std;
int main()
{
int row,col,num;
cout<<“Enter range of rows:”;
cin>>num;
for(row=num;row>=1;row–)
{
for(col=num;col>=row;col–)
{
cout<<row<<” “;
}
cout<<“n”;
}
return 0;
}
Output-
Enter a range of rows:5
5
4 4
3 3 3
2 2 2 2
1 1 1 1 1
Program Explanation –
First of all, we have declared the variable row, col, and num .which we are going to use in the program.
Then we have taken a number of rows as input from the user.
Then we started first, the outer For loop to print rows.
Then we started the second and inner for loops to print columns inside the outer loop.
Then we printed the output of the program in the inner loop.
In the end, we have applied a new line character ‘/n’ in the outer loop.
Conclusion –
Here we have seen the program in which we are going to print the pattern the pattern is very simple and easy. We hope that you have understood the program and you will explore it even more.