How to limit integers per line (for loop)

The loop is to print all the even numbers from 100-200, with 10 per line. Currently it is printing in a triangle formation. I am unsure how to limit the number per line to 10, if anyone could help I would greatly appreciate it.
1
2
3
4
5
6
7
8
int num1;
int num2;

for (num1 = 100; num1 < 200; num1 += 2) {
		for (num2 = 100; num2 <= num1; num2 += 2)
			cout << num2;
			cout << "\n";
	}
Here's one way you could do it:

1
2
3
4
5
6
7
#include <iostream>

int main() {
    for (int x=100, y=1; x <200; x+=2, y++)
	  (y%10) ? (std::cout << x << "\t"):(std::cout << x << "\n");
return 0;
}

Output:
100	102	104	106	108	110	112	114	116	118
120	122	124	126	128	130	132	134	136	138
140	142	144	146	148	150	152	154	156	158
160	162	164	166	168	170	172	174	176	178
180	182	184	186	188	190	192	194	196	198


You really don't need two loops to do what you want, and it seems simple and practical just to use a separate counter (y) to keep track of when to do the new-line, although I'm sure there are many other ways too.

Hope that helps.
Last edited on
Topic archived. No new replies allowed.