Sep 30, 2012 at 8:56pm UTC
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 Sep 30, 2012 at 8:57pm UTC
Sep 30, 2012 at 9:06pm UTC
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 Sep 30, 2012 at 9:06pm UTC
Sep 30, 2012 at 9:08pm UTC
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 Sep 30, 2012 at 9:21pm UTC
Sep 30, 2012 at 9:12pm UTC
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.
Sep 30, 2012 at 9:18pm UTC
works flawlessly
Thanks guys!