Home  >  Article  >  Backend Development  >  Find the sum of an arithmetic sequence of staggered signs

Find the sum of an arithmetic sequence of staggered signs

WBOY
WBOYforward
2023-09-16 17:01:021174browse

Find the sum of an arithmetic sequence of staggered signs

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

Let us understand AP with an example sequence,

5, 7, 9, 11, 13, 15, . . . The tolerance (d) of this arithmetic series is 2. This means that each subsequent element differs from the previous element by 2. The first item (a) in this sequence is 5.

The general formula to find the nth term is a{n} = a (n-1)(d)

In this problem, we are given an AP and we need to find the alternating The sum of the series of signed squares, the series will look like this,

a12 - a22 a32 - a42 a52

Let us give an example for a clearer understanding −

Input: n = 2
Output: -10

Explanation

’s Chinese translation is:

Explanation

12 - 22 + 32 - 42 = -10

Example

’s Chinese Translates to:

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

The above is the detailed content of Find the sum of an arithmetic sequence of staggered signs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete