for
Create a triangle type pattern
Write a C++ program that asks the user to enter a number of rows to be printed. It should then display for the first row one asterisk preceded by periods. The second row should display two asterisks preceded by periods and so on until all the rows have been printed as entered by the user.
A sample run would be like so:
....*
...**
..***
.****
*****
I've seen many varied questions surrounding this particular problem.
Here's a solution, firstly, just the logic of the code for clarity, not concerning ourselves with error trapping incorrect keyboard input. The object is to understand the reasoning of the logic of addressing the problem at hand.
// 29 Nov, 2007.
#include <iostream>
using namespace std;
int main()
{
int rows, c;
cout << "Enter the number of rows: ";
cin >> rows;
cin.get();
for (int r = 1; r <= rows; r++)
{
for (c = 1; c <= rows - r; c++)
cout << ".";
for (; c <= rows; c++)
cout << "*";
cout << endl;
}
// exit routine
cout << "\n\n...Press 'ENTER' key to EXIT...\n";
cin.get();
return 0;
}
Copy and paste the above into your compiler of choice, compile and run it. As you can see, it works.


Recent comments
10 min 39 sec ago
23 hours 58 min ago
1 day 5 hours ago
1 day 16 hours ago
1 day 16 hours ago
3 weeks 6 days ago
3 weeks 6 days ago
4 weeks 49 min ago
10 weeks 1 day ago
11 weeks 4 days ago