Write your question here.
v:\cmpsc101>prelab8
Enter an integer m:
2
Enter integer numbers, enter 0 to terminate:
1
3
4
5
0
3 numbers are not multiples of 2.
it's gotta be like this when printed out.
I did the loop correctly, but now I am stuck on finding how many numbers the user entered are not multiples of m. how can i find y(the number of user typed that are not multiples of m.
#include <iostream>
using namespace std;
int main () {
int m;
int x;
int y;
cout << "Enter an integer m:\n";
cin >> m;
cout << "Enter integer numbers, enter 0 to termintate:\n";
while (x!=0) {
cin >> x;
}
cout << y << " numbers are not multiples of " << m << ".\n";
return 0;
}
After getting x, immediately check whether its a multiple of m using the modulo operator "%".
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//example
int m;
int x = -1; //Set this to an initial value. Else you'll have a garbage value.
int y = 0; //Need to set this to 0.
//get m
cin >> m;
//get x and validate it
while(x != 0)
{
cin >> x; //get x
if(x%m != 0) //if the remainder of x/m isn't 0 means x isn't a multiple of m.
y += 1; //Increase y by 1.
}
cout << y << " numbers are not multiples of " << m << ".\n";