In this post, we will learn CPP Program to C++ Program to print triangle patterns.Let us understand with different c++ example codes
1.C++ Program to draw triangle Pyramid of start(*)
In this program, we are drawing a half Pyramid in the CPP program.
Program Example
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter number of rows for triangle pyramid: ";
cin >> rows;
for(int x = 1; x <= rows; ++x)
{
for(int y = 1; y <= x; ++y)
{
cout << "* ";
}
cout << "\n";
}
return 0;
}
Output
Enter number of rows for triangle pyramid: 5
*
* *
* * *
* * * *
* * * * *
2. C++ Program to Draw a triangle pattern of Number
In this program, we are drawing a half Pyramid of numbers in the CPP program.
Program Example
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter number of rows for triangle pyramid: ";
cin >> rows;
for(int x = 1; x <= rows; ++x)
{
for(int y = 1; y <= x; ++y)
{
cout <<y <<" ";
}
cout << "\n";
}
return 0;
}
Output
Enter number of rows for triangle pyramid: 5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
3. Program to Draw inverted triangle pattern
In this program, we are drawing an inverse half Pyramid in the CPP program.
Program Example
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter number of rows for triangle pyramid: ";
cin >> rows;
for(int x = rows; x >= 1; --x)
{
for(int y = 1; y <= x; ++y)
{
cout <<"* ";
}
cout << "\n";
}
return 0;
}
Output
Enter number of rows for triangle pyramid: 5
* * * * *
* * * *
* * *
* *
*
4. Program to Draw inverted triangle pattern of number
In this program, we are drawing an inverse half Pyramid of numbers in the CPP program.
Program Example
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter number of rows for triangle pyramid: ";
cin >> rows;
for(int x = rows; x >= 1; --x)
{
for(int y = 1; y <= x; ++y)
{
cout <<y<<" ";
}
cout << "\n";
}
return 0;
}
Output
Enter number of rows for triangle pyramid: 5
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1