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:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream.h>
#include "math.h"
#include <conio.h>
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream.h>
#include "math.h"
#include <conio.h>

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

1
2
3
4
5
6
7
8
9
10
int n = 3;
int sum = 0;

while ( n < 100 && n % 2 )
{
   sum += n;
   n += 3;
}

cout << "Sum = " << sum << endl;


Or another way

1
2
3
4
5
6
7
8
9
10
int n = 3;
int sum = 0;

while ( n < 100 )
{
   sum += n;
   n += 6;
}

cout << "Sum = " << sum << endl;

Last edited on
Very quick answers, i had a thought about that ''while'' command but had no idea how to put that together,

running total is something completely new to me.

works flawlessly

Thanks guys!
Topic archived. No new replies allowed.