im trying to write a c++ program that can do this equation

s= +1-2-3+4+5+6-7-8-9-10+11+12+13+14+15...................n

it first started with one + then 2 -- then 3 +++ then 4----
1
2
3
4
5
6
7
8
9
10
11
12
13
int op_limit = 1;
int op_done = 0;
bool add = true;
int sum = 0;
for(int i = 1; i <= n; ++i) {
    if(op_done == op_limit) {
        op_done = 0;
        ++op_limit;
        add = !add;
    }
    sum += (add? t : -t);
    ++op_done;
}
http://ideone.com/kuxkNB
Topic archived. No new replies allowed.