Program reports number of characters to make shape

Hello, first time here. I have a program that creates a half pyramid shape using the "*" symbol. The number of rows the pyramid has is determined by the user. Can someone help me with my next step? My next step is add a function that will to have the program tell the user how many "*" symbols it took for it to make the half pyramid. Thank you

My 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;
}
just put a variable into second for loop that will count asterisks until (rows * 0.5) and then show how much that is.
Last edited on
cout << rows * ( 1 + rows) / 2;
Both of you, thank you for helping me. I know this might have been simple but this material is hard for me to understand. One more request. When I run the program, how can I get it to tell the user "There are "#" of asterisks." ?
cout << "There are " << rows * ( 1 + rows ) / 2 << " asterisks\n";

Just before your return 0; statement.


Voilá
Enter number of rows: 4
* 
* * 
* * * 
* * * * 
There are 10 asterisks
Last edited on
@lastchance
I think the OP want's to know how many asterisks there are to fill half of the piramide, not how many asterisks there are in total.
@malibor, which half of the "pyramid" would you like to fill? What answer would you give if rows=1 or rows=2?
I don't know, maybe the output in that case would be, "unable to calculate" or show decimal result as an estimation.
@malibor,
I suspect that the OP meant that this is a half pyramid:
*
* *
* * *
* * * *


whilst this is a whole pyramid:
      *
    * * *
  * * * * * 
* * * * * * *

Ah yes, you're correct! I got the OP question wrong xD
Topic archived. No new replies allowed.