Addition ?

Hi I have a problem that needs to be solved in C++
It is to input an integer (n)

and then output this sum to the screen :

S= 3-5+7-9+11- .... +- n

example :

if I input n=9 I get :
S =-4

Please help me thanks :)
Well, there is a way without loops, but I'll leave that for you to see.
You code will look like
1
2
3
4
5
6
7
int term, n, i = 1, sum=0;
cin >> n;
do{
   /*calculate term using i*/
   i++;
   sum += term;
}while(term < n);

If you don't see how to calculate term, draw yourself a table of values of i and term. It's really simple. While doing it, you'll probably need to check if i is odd or even. You do that by checking the remainder or division by two : if (i%2 == 0) //i is even .
thanks a lot
Topic archived. No new replies allowed.