Oct 28, 2020 at 6:33pm
I assume you mean
Think of it like printing two distinct characters, except the o's become spaces:
*****
o****
oo***
ooo**
oooo* |
So, every row, you need to print X amount of the first character, and (length - X) amount of the second character.
1 2 3 4 5 6 7 8 9
|
for (each row)
{
for (X amount of times)
print 'o'
for (row - X amount of times)
print '*'
print newline
}
|
Once you see the pattern and can print it, change the 'o's back to spaces.
Last edited on Oct 28, 2020 at 6:46pm
Oct 28, 2020 at 8:32pm
You only need 1 for loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int row;
cout << "Enter an integer: ";
cin >> row;
for (int i = 0; i < row; ++i)
cout << setfill('*') << setw(row - i + 1) << '\n';
}
|
or for right aligned,
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int row;
cout << "Enter an integer: ";
cin >> row;
for (int i = 0; i < row; ++i)
cout << setfill(' ') << setw(i) << "" << setfill('*') << setw(row - i + 1) << '\n';
}
|
Last edited on Oct 29, 2020 at 9:22am