Printing consecutive integers.
I need to print the integer 1-9 in this way:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9 |
I have the spaces all worked out, but I don't know how to do the numbers. Here's my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <iostream>
using namespace std;
int main ()
{
const int NUMLINES=9;
for (int space=16; space>=0; space=space-2)
{
for (int spaceOutput = space; spaceOutput>0; spaceOutput=spaceOutput-2)
{
cout <<" ";
}
for (int num=1; num<=9; num++)
{
cout << num << " ";
}
cout << endl;
}
return 0;
}
|
It currently displays the correct spacing, but gives me 1-9 on every line.
Topic archived. No new replies allowed.