### Getting a sum of these numbers [c++]

How can i calculate the SUM of all these numbers that show up? the numbers are from 1-100 which can be divided by 3 but at the same time CANNOT be divided by 2.

SO its 3,9,15,21 and so on

Here is the code:

 ``123456789101112`` ``````#include #include "math.h" #include int main() { clrscr(); int n; for(n=1;n<=33;n+=2) { cout << 3*n << endl; } getch(); }``````

Now i need to get the SUM of all these numbers so 3+9+15...
Last edited on
 ``12345678910111213141516`` ``````#include #include "math.h" #include int running_total = 0; int main() { clrscr(); int n; for(n=1;n<=33;n+=2) { running_total += 3*n cout << 3*n << endl; } getch(); }``````
Last edited on
For example it can be done the following way

 ``12345678910`` ``````int n = 3; int sum = 0; while ( n < 100 && n % 2 ) { sum += n; n += 3; } cout << "Sum = " << sum << endl;``````

Or another way

 ``12345678910`` ``````int n = 3; int sum = 0; while ( n < 100 ) { sum += n; n += 6; } cout << "Sum = " << sum << endl;``````

Last edited on