math and showing it in C++

So i got a task asking to use "for" interval to show numbers from 1-100 which can be divided by 3, but CANNOT be divided by 2.

Afterwards calculate the sum of all these numbers.

I got this far but have no clue on the formula to show only numbers that can be divided by 3 and at the same time can't be divided by 2.

1
2
3
4
5
6
7
8
#include <iostream.h>
#include <math.h>
#include <conio.h>
int main()
{
clrscr();
int i;
for(i=1; i>=100; i++)


Any help appreciated.
Last edited on
You can describe all numbers that can be divided by 3 with x = 3 * n, n being an integer. This value can only be divided by 2, if n is a multiple of 2, i.e. x = 3 * (2 * m), with m = n / 2. So if n is odd, x will be an integer that can be divided by 3, but can not be divided by 2.

Examples:
n = -1: x = 3*n = -3 < 1
n = 1: x = 3*n = 3
n = 3: x = 3*n = 9
n = 5: x = 3*n = 15
...
n = 33: x = 3*n = 99
n = 35: x = 3*n = 105 > 100

So you need to start the loop with n=1 and increase n by two after each time executing the loop

1
2
3
for(n=1;n<=33;n+=2) {
//calculate x = 3*n
}


Edit: Of course you could alternatively run the loop from 1 to 100 and check each time if i can be divided by 2 and 3 using the modulo operator:

1
2
3
4
5
6
if(i%3==0) {
    //i can be divided by 3
    if(i%2!=0) {
        //i can not be divided by 2
    }
}
Last edited on
Thanks alot

im gonna try em out now.
Last edited on
I suspect the use of the modulus operator is what is intended.

Still, I was going to suggest start at 3, and then increment in steps of 6.
i am soo stupid for this if i do the 1st suggestion,

after putting cout<<i im only getting a number "35"

instead of 3, 9, 15

dasdasdsa my head hurts, lol
Try this:

1
2
3
for(n=1;n<=33;n+=2) {
    cout << 3*n << endl;
}
oh wow it worked!

thanks wouldn't be able to do it without you.
Topic archived. No new replies allowed.