Star Pyramid
Completed
Read n and print a centred pyramid of n rows: row i has n−i spaces and 2i−1 stars. Use string(count, ch).
Input
One integer.
Output
n rows.
Example
Input
4
Output
* *** ***** *******
Editor
C++
Hints
string(5, '*') is "*****".
Solution explanation
The string(count, char) constructor replaces an inner loop.
Show solution
#include <iostream>
#include <string>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cout << string(n - i, ' ') << string(2 * i - 1, '*') << "\n";
}
return 0;
}