I am really confused on how to to this pattern. (this must be the output with cin)
________1
______1 2
____1 2 3
__1 2 3 4
1 2 3 4 5
(the underscore represents spaces)
Last edited on
for each line
you have to show X spaces
and Y numbers |
"for each" sounds like a loop, does it not?
Okay, a loop that repeats as many times as there are lines.
The loop's counter has "number of current line"?
What to do for one line?
How to "show X spaces"? With a loop:
How to "show Y numbers"? With a loop:
Newline.
You can compute X and Y from the number of current line.
Last edited on
Thanks! Your explanation is really a big help. What if I need the user to input the number of rows?
#include <iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter number of rows: ";
cin >> rows;
for(int i = rows; i >= 1; --i)
{
for(int j = 1; j <= i; ++j)
{
cout << j << " ";
}
cout << endl;
}
return 0;
}