-
Simple while loop
Sorry to be wasting your time with this simple problem, but I'm in my first class and am stumped on a while loop.
Here is what I have:
Code:
#include <iostream>
int main() {
int i = 100;
while (i <= 150){
cout << i << " ";
i = i + 2;}
return 0;}
The program runs just fine, but the numbers all appear on one line. I need to have only 6 per line, for example 100 102 104 106 108 110
112 114 116 etc.... to 150
Keep in mind I have to use a while loop. I know I need to add another variable somewhere in that loop but I cant figure out the syntax I need to use. Can someone give me a push in the right direction? Thank you very much.
-
First of all this is C++ not C, so please post under the appropriate forum. Admins will probably move the thread there.
Secondly, all you need is another variable, let's call it x that you initialize to 0 before the loop. Then, inside the loop you increment and test if it's new value % 6 == 0. That means that the variable value has reached a value which is a multiple of 6 and it's time to print an endline:
i.e. cout << endl;
-
Oh yes, so I have moved it.