(1) Use the for statement and separate the calculation and summation with odd and even numbers # include <iostream>
Using namespace STD;
Int main ()
{
Int I, J;
Int sum = 0, sum1 = 0, sum2 = 0; // The value must be initialized to 0 before summation. Otherwise, a strange value is returned.
For (I = 1; I <= 100; I ++, I ++) // the original for statement can be used in this way
{
Sum1 + = I;
}
For (j =-2; j> =-100; j --, j --) // the original for statement can be used in this way.
{
Sum2 + = J;
}
Sum = sum1 + sum2;
Cout <sum <Endl;
Return 0;
}
(2) combined with the if statement, use a judgment to separate addition and subtraction: # include <iostream>
Using namespace STD;
Int main ()
{
Int sum = 0;
For (INT I = 1; I <= 100; I ++)
{
If (I % 2 = 1)
{
Sum + = I;
} Else
{
Sum-= I;
}
}
Cout <sum <Endl;
Return 0;
} (3) Simpler Method # include <iostream>
Using namespace STD;
Int main ()
{
Int sum = 0;
For (INT I = 1; I <= 100; I ++)
{
If (I % 2 = 1)
{
I =-I;
}
Sum + = I;
}
Cout <sum <Endl;
Return 0;
}