Table of Contents
Example
Output
Home Backend Development C++ In C programming, sum the first N items of the sequence 2, 6, 12, 20, 30

In C programming, sum the first N items of the sequence 2, 6, 12, 20, 30

Aug 26, 2023 pm 04:05 PM
c programming first n items Sequence sum

In C programming, sum the first N items of the sequence 2, 6, 12, 20, 30

To require the sum of this series, we first analyze this series.

The series is: 2,6,12,20,30...

Example

For n = 6
Sum = 112
On analysis, (1+1),(2+4),(3+9),(4+16)...
(1+1<sup>2</sup>), (2+2<sup>2</sup>), (3+3<sup>2</sup>), (4+4<sup>2</sup>), can be divided into two series i.e.
s1:1,2,3,4,5&hellip; andS2: 1<sup>2</sup>,<sup>2</sup>,3<sup>2</sup>,....
Copy after login

Use mathematical formulas to find the sum of the first and second

Sum1 = 1+2+3+4&hellip; , sum1 = n*(n+1)/2
Sum2 = 12+2<sup>2</sup>+3<sup>2</sup>+4<sup>2</sup>&hellip; , sum1 = n*(n+1)*(2*n +1)/6
Copy after login

Example

#include <stdio.h>
int main() {
   int n = 3;
   int sum = ((n*(n+1))/2)+((n*(n+1)*(2*n+1))/6);
   printf("the sum series till %d is %d", n,sum);
   return 0;
}
Copy after login

Output

The sum of series till 3 is 20
Copy after login

The above is the detailed content of In C programming, sum the first N items of the sequence 2, 6, 12, 20, 30. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use C++ to write code to find the Nth non-square number Use C++ to write code to find the Nth non-square number Aug 30, 2023 pm 10:41 PM

Use C++ to write code to find the Nth non-square number

Find the number of unique pairs in an array using C++ Find the number of unique pairs in an array using C++ Sep 07, 2023 am 11:53 AM

Find the number of unique pairs in an array using C++

Inversion algorithm for right rotation of array written in C++ Inversion algorithm for right rotation of array written in C++ Sep 08, 2023 pm 08:17 PM

Inversion algorithm for right rotation of array written in C++

In C programming, find the area of ​​a circle In C programming, find the area of ​​a circle Aug 25, 2023 pm 10:57 PM

In C programming, find the area of ​​a circle

Reverse doubly linked list grouping by given size using C++ Reverse doubly linked list grouping by given size using C++ Sep 04, 2023 am 09:49 AM

Reverse doubly linked list grouping by given size using C++

Write a code using C++ to find the number of subarrays with the same minimum and maximum values Write a code using C++ to find the number of subarrays with the same minimum and maximum values Aug 25, 2023 pm 11:33 PM

Write a code using C++ to find the number of subarrays with the same minimum and maximum values

Reversal algorithm for array rotation written in C++ Reversal algorithm for array rotation written in C++ Aug 28, 2023 pm 11:13 PM

Reversal algorithm for array rotation written in C++

Written in C++, find the number of reflexive relations on a set Written in C++, find the number of reflexive relations on a set Aug 26, 2023 pm 08:17 PM

Written in C++, find the number of reflexive relations on a set

See all articles