You can play games with modular arithmetic to avoid adding another variable, but sometimes it's just easier to count how many numbers you've printed. When you get to 3, print a newline and reset the counter:
#include <iostream>
using namespace std;
int main()
{
int x, n = 0;
cout<<"Numbers between 5 and 12 (3 numbers per line)are:\n";
for ( x = 5; x <= 12; x++ )
{
cout<< x <<" ";
if ( ++n == 3 ) {
cout<<'\n';
n = 0;
}
}
cout<<"\n\n";
}
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Offline 11,807 posts
since Sep 2004