FOR loop with Multiple message.

Just printing 1-100 but at every multiple of 10 i want to input message "Multiple of 10" next to the number. So far i can only get it to say it next to all the numbers. Does anyone know what i am missing?


#include <iostream>
using namespace std;
int main()
{
int x = 1;
for(x = 1; x <= 100; x++) {
cout << x;
if(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
cout << "Multiple of 10" << endl;
}






system("pause");
return 0;
}
You need the modulus operator.

1
2
3
4
5
6
7
8
for ( int x = 1; x <= 100; ++x )
    {
    cout << x << ' ';
    if ( (x % 10) == 0)
        {
        cout << "Multiple of 10" << '\n';
        }
    }




The modulus operator returns the remainder of a division. So if x is 10, then 10/10 = 1, with a remainder of 0.

If x is 13. Then 13/10 = 1, with a remainder of 3. So 3 is not == 0 and won't trigger the if statement.
Last edited on
thanks very much. that worked very well. i wish i could have found that in my C++ book. if i may? the program writes the message under the multiple, i tried numerous times to shift it to appear next to the number instead of under but no luck.
get rid of the "endl" or '\n' character, both of those move the text onto a new line (although endl is slower because it flushes the buffer each time)
Topic archived. No new replies allowed.