Sum of series with alternate signed squares of AP

CServer Side ProgrammingProgramming

An arithmetic progression (AP) is a series of numbers in which the difference between two consecutive terms in the same. The difference is calculated by subtracting the second term from the first.

Let's take a sample sequence to know about AP,

5, 7, 9, 11, 13, 15, . . . The common difference(d) of this arithmetic progression is 2. This means every succeeding element differs the former one by 2. The first term (a) of this series is 5.

The general formula for finding the nth term is a{n} = a + (n-1)(d)

In this problem, we are given an AP and we need to find the sum of series with alternate signed square, the series will look like,

a12 - a22 + a32 - a42 + a52 +......

Let's take an example, to make this more clear −

Input: n = 2
Output: -10

Explanation

12 - 22 + 32 - 42 = -10

Example

#include <stdio.h>
int main() {
   int n = 4;
   int a[] = { 1, 2, 3, 4, 5, 6, 7, 8}; int res = 0;
   for (int i = 0; i < 2 * n; i++) {
      if (i % 2 == 0) res += a[i] * a[i]; else res -= a[i] * a[i];
   }
   printf("The sum of series is %d", res);
   return 0;
}

Output

The sum of series is -36
raja
Published on 19-Aug-2019 12:03:11
Advertisements