Printing n rows of digits (nested loops)

Hello all,

I'm beginning to learn C++, and I need to write a program that prints n rows of digits. The nth row needs to read 1234...n. Also, if a row has more than 10 digits, the digit after 9 should start from 0. For example:

If n = 12, output should be:

1
12
123
1234
12345
123456
1234567
12345678
123456789
1234567890
12345678901
123456789012

My code so far is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>                                      
using namespace std;
int main()
{
double length;
double row;
double col;

cout << "Enter the number of rows (integer): ";
cin >> length;

for (row = 1; row <= length; row++)
{
   for (col = 1; col <= row; col++)
     {
       cout << col << endl;
     }
   cout << " " << endl;
}
return (0);
}

My code outputs:
1

1
2

1
2
3

1
2
3
4

1
2
3
4
5
(ect...)

For numbers greater than 10, I need to use x%10 to compute x mod 10.

I'm not sure how to get the output horizontal rather than vertical. I also don't have a great grasp of loops within loops, which probably contributes to my problem. Can anyone help me out? Thanks
Last edited on
First: use code tags, please. Second: Hint, Try to remove one endl. Third: Another hint, use x mod 10 always
eraggo, thanks for the help. I got it now
I'm working on the same problem, but I'm absolutely stumped as to where the x mod 10 is supposed to be used. Any help would be appreciated.
Topic archived. No new replies allowed.