


In C programming, sum the first N items of the sequence 2, 6, 12, 20, 30
Aug 26, 2023 pm 04:05 PMTo 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… andS2: 1<sup>2</sup>,<sup>2</sup>,3<sup>2</sup>,....
Use mathematical formulas to find the sum of the first and second
Sum1 = 1+2+3+4… , sum1 = n*(n+1)/2 Sum2 = 12+2<sup>2</sup>+3<sup>2</sup>+4<sup>2</sup>… , sum1 = n*(n+1)*(2*n +1)/6
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; }
Output
The sum of series till 3 is 20
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

In C programming, find the area of a circle

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

Reversal algorithm for array rotation written in C++

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