question about creating a five mod sum

So I need some help figuring out how to do this, this was the prompt given:

Write a program that accepts an input integer n, and calculates the number and sum of all the numbers between 1 and n (inclusive) that are NOT evenly divisible by ANY of the first 5 prime numbers (2,3,5,7,11). The program should print out a clearly labeled count and sum.

I understand how to have a cout and cin for an integer n, and I understand the form of mod how you do 5 % 4 = 1, but I'm not sure how to do the part after (inclusive).

I have to use a while statement and I was thinking of using if statements as well.
Perhaps a for loop would work better than while, at least just for understanding of the concept.
Think of this:
1
2
3
4
5
6
7
8
9
int sum=0, countofnumbersadded=0;
for(int counter = 1; counter <= n; counter++)
{
    if (counter%2!=0 && counter%3!=0 && counter%5!=0 && counter%7!=0 && counter%11!=0)
    {
        sum+=counter;
        countofnumbersadded++;
    }
}

The % returns the remainder, so if you have a zero remainder, then you have an even divisor. (The number fits perfectly with nothing left over.)
If you check to make sure that the counter is not evenly divisible by 2, 3, 5, 7, and 11 (as in the remainder is not equal to zero, which means it is not evenly divisible), you can the add that counter to your ongoing sum. Then you should increment the counter by 1 and continue on checking and adding until your number reaches the n that was inputted by the user.
Last edited on
Topic archived. No new replies allowed.