5 values in one line


how do i write 5 values per line in a c+= program where i have to print the numbers from 1 to 100? so far i have done the following.i am a super beginner .can anyone please help?
1
2
3
4
5
6
7
8
9
10
11
  #include <iostream>

using namespace std;

int main()
{
    int number;
    for (number=1;number<=100;number++){
        cout<<number<< " " <<endl;
    return 0;
}
wildchild think about adding to the variable you are printing on the screen using the ++ operator.

such as:
1
2
3
4
5
    int number;
    for (number=1;number<=100;number++)
   {
        cout<<number++<< " " << number <<endl;
   }


the code above will do two per line. Now take what i have shown and make it do the same thing five times
cout<<number++<< " " << number <<endl;
Results are undefined as order of argument evaluation is unspecified. You cannot be sure if it will output, say 1, 2 or 1, 1.

There are two easy ways, first one is to print numbers one by one and enter a newline each fifth number:
1
2
3
4
5
for(int number = 1; number <= 100; ++number) {
    std::cout << number << ' ';
    if(number & 5 == 0)
        std::cout << '\n';
}
Second one is close to OUIJ suggestion, print five at time:
1
2
for(int n = 1; n <= 100; n += 5)
    std::cout << n << n + 1 << n + 2 << n + 3 << n + 4 << '\n';
i got it. Thank you everyone.But MiiniPaa ,what's '\n' for?
Wildchild, '\n' goes to the next line
The std::endl is more than just endline: http://www.cplusplus.com/reference/ostream/endl/

The trailing whitespace is annoying. Hence a variation on the theme:
1
2
3
for ( int number = 1; number <= 100; ++number ) {
    std::cout << number << (( 0 == number % 5 ) ? '\n' : ' ');
}

That uses the ternary operator.

Another note:
1
2
" " // is a two-byte string literal {' ', '\0'}; const char *
' ' // is a single char 
Topic archived. No new replies allowed.