Programs to Print Triangles Using *.
Example 1: Program to Print a 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: Inverted Half-Pyramid Using *
* * * * *
* * * *
* * *
* *
*
Source Code
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = rows; i >= 1; --i) {
for(int j = 1; j <= i; ++j) {
cout << "* ";
}
cout << endl;
}
return 0;
}