Home > Backend Development > C++ > body text

Find the sum of an arithmetic sequence of staggered signs

WBOY
Release: 2023-09-16 17:01:02
forward
1117 people have browsed it

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
Copy after login

Explanation

’s Chinese translation is:

Explanation

12 - 22 + 32 - 42 = -10
Copy after login

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;
}
Copy after login

Output

The sum of series is -36
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!