C++ Primer Plus Chapter 5 Exercise 9

c plus plusThis is an interesting program that takes a second to think about. The issue here is logic.  Basically, we gather the number of rows we want and compare it in our first for statement. It’s going to be greater then zero so we enter our loop and see another for statement.  If ‘d” is still less than rows – less, we output a decimal point. we then enter out next for statement which looks at integer “a” and compares our “less” value against it. The resulting number is how many asterisk are outputted after out decimal points. We then increment “less” until we reach our rows value, as checked in our first for statement.

9. Write a program using nested loops that asks the user to enter a value for the number of
rows to display. It should then display that many rows of asterisks, with one asterisk in
the first row, two in the second row, and so on. For each row, the asterisks are preceded
by the number of periods needed to make all the rows display a total number of characters
equal to the number of rows. A sample run would look like this:
Enter number of rows: 5
….*
…**
..***
.****
*****

#include <iostream>

using namespace std;

int main()
{
int rows = 0;
int less = 1;

cout << "Enter number of rows: ";
cin >> rows;

for(int i=0; i < rows; i++)
{
for(int d=0; d < (rows - less); d++)
cout << ".";
for(int a=0; a < less; a++)
cout << "*";

cout << endl;
less++;
}
return 0;
}