Write C++ Programs to display the following triangle
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Answers
Answer:
Program to print half pyramid using *
*
* *
* * *
* * * *
* * * * *
Source Code
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; ++i)
{
for(int j = 1; j <= i; ++j)
{
cout << "* ";
}
cout << "\n";
}
return 0;
}
Example 2: Program to print half pyramid a using numbers
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Source Code
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = 1; i <= rows; ++i)
{
for(int j = 1; j <= i; ++j)
{
cout << j << " ";
}
cout << "\n";
}
return 0;
}
Explanation: