How To Make Increasing And Decreasing Triangular Patterns Using C++
Hello everyone I hope you will be well and good.
Today I am here to share some increasing and decreasing triangle patterns Patterns with the help of the C++. I will teach you with their basic logic. It will be an informative post for the beginners in programming and coding.
Introduction to For Loop
I will be using only for loop to make the pyramid and patterns. So firstly I want to tell you about the for loop.
For loop is a control loop and it controls the number of the ietrations. And in these structures we have to work with ietrations. The first for loop is initialised once and then the remaining for loops are executed completely. Then the control again comes to the for loop and it allows for the next ietration and so on. So in this way the for loop works.
Here:
int is the data type which is used to declare the integer variables. This data type may be changed according to the input values. If we are dealing with a decimal value then we use float and so on.
i<=5 is the condition which determines the number ietrations of the loop.
i is the unary operator which is used for the increment. And it is the work of this operator which increases the value of i after each iteration and the next is proceeded.
How to make the Patterns
So firstly I want to tell you how we can make an increasing and decreasing half triangle pattern using for loop in c++. I will make these patterns in simple numbers and then with the help of the stars.
And now I am going to tell you that I am not finding the number of rows but I will get the numbers of rows from the user.
#include
using namespace std;
int main()
{
int rows,i,j;
cout << "Enter number of rows: ";
cin >> rows;
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= i; j++)
{
cout<<j;
}
cout<<endl;
}
return 0;
}
So it is the half increasing triangle in the integers are making it.
#include
using namespace std;
int main()
{
int rows,i,j;
cout << "Enter number of rows: ";
cin >> rows;
for(i = 1; i <= rows; i++)
{
for(j = 1; j <= i; j++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
So the code is ready for the first half increasing triangle pattern.
So our output is here and we have successfully made the first half increasing triangle pattern. You can see that I have given just 5 numbers of rows. So the five rows have been produced.
In order to make the decreasing pattern triangle we have to change the conditions in the second loop. As the first loop control the number of rows so it will remain same but the second loop will changed.
As we have to make the decreasing pattern so in the first row there should be maximum columns. So I have to start the second loop from the maximum value as compared to the rows. And then in the condition I have to put in such a way that after each iteration the value of coloumns should decrease with increas in the rows.
for(j = rows; j >= i; j--)
so it is the new condition to make the decreasing pattern of the triangle.
#include
using namespace std;
int main()
{
int rows,i,j;
cout << "Enter number of rows: ";
cin >> rows;
for(i = 1; i <= rows; i++)
{
for(j = rows; j >= i; j--)
{
cout<<j;
}
cout<<endl;
}
return 0;
}
And after the change in the second loop it is the new code for the decreasing pattern of the triangle.
And now it is the output in the form of the numbers and you can see in the first row the coloumns are maximum and then these are decreasing turn by turn.
#include
using namespace std;
int main()
{
int rows,i,j;
cout << "Enter number of rows: ";
cin >> rows;
for(i = 1; i <= rows; i++)
{
for(j = rows; j >= i; j--)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
And now in order to obtain the staric pattern of the triangle I have just replaced the output with the cout<<j
to cout<<"*"
and the remaining program is same as of above.
So in this way we can make increasing as well as decreasing half triangle patterns using c++. If you have any confusion then you can ask.
Yours: @uop
This post has been manually curated by @bhattg from Indiaunited community. Join us on our Discord Server.
Do you know that you can earn a passive income by delegating to @indiaunited. We share 100 % of the curation rewards with the delegators.
Here are some handy links for delegations: 100HP, 250HP, 500HP, 1000HP.
Read our latest announcement post to get more information.
Please contribute to the community by upvoting this comment and posts made by @indiaunited.
Thank you to the whole team and the community.
Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!
Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).
You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.
Thank you for your support 😘
Nicee!!! you're smart and nice how well you broke things down. You'd make a fine Software Engineer. Bravo Uop🔥👏
Wow! Nice uop. So you are devloper!
Hehe nice to know it.
Congratulations, your post has been upvoted by @dsc-r2cornell, which is the curating account for @R2cornell's Discord Community.