Nov 5, 2014 at 8:31pm UTC
Hey, I want to output numbers llike this:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 etc
I write such code:
1 2 3 4 5 6 7 8 9 10
int i=0;
int j = 0;
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++)
{
cout << j;
}
cout << endl;
}
But it outputs like this:
1
1 2
1 2 3
1 2 3 4
How to output the way I want?
Thanks
Last edited on Nov 6, 2014 at 7:11am UTC
Nov 5, 2014 at 8:45pm UTC
What if you had another variable in there, like int n = 1;
that you printed inside your loops? (Declare it before the loops, just like i
and j
.)
Hope this helps.
Last edited on Nov 5, 2014 at 8:46pm UTC
Nov 5, 2014 at 8:46pm UTC
Looks to me like you are on the right track. You could:
On line 3 create another int e.g. int number = 1;
On line 7, instead of outputting j
, output number
After line 9, increment number
(i.e. number++;
)
Edit: D'oh, too slow.
Last edited on Nov 5, 2014 at 8:48pm UTC
Nov 5, 2014 at 8:57pm UTC
It happens to me all the time.
By the way, my post count is now a prime number. (But the next time you read this the likelihood that it is still prime is around 12%.)
Nov 6, 2014 at 7:32am UTC
Yeah, it works.
Thanks guys :)