# Sum of series with alternate signed squares of AP

CServer Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

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
Updated on 19-Aug-2019 08:33:11