Explanation:
In the series to be generated, odd numbers are +ve and even numbers are −ve. Take one variable k = 1. k is multiplied by −1 inside the loop in each iteration.
Initially sum = 0 + (1 * 1) gives sum = 1 then k becomes k = 1 * − 1= −1, which is used in the second iteration of the loop. In the second iteration, sumbecomes sum = 1 + (2 * −1) => sum = 1 − 2 = − 1 and value of k changes to 1 again (−1 * −1 = 1) and this continues for n times.