search
HomeBackend DevelopmentC++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

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>,....

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

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!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
使用C++编写代码,找到第N个非平方数使用C++编写代码,找到第N个非平方数Aug 30, 2023 pm 10:41 PM

我们都知道不是任何数字的平方的数字,如2、3、5、7、8等。非平方数有N个,不可能知道每个数字。因此,在本文中,我们将解释有关无平方数或非平方数的所有内容,以及在C++中查找第N个非平方数的方法。第N个非平方数如果一个数是整数的平方,则该数被称为完全平方数。完全平方数的一些例子是-1issquareof14issquareof29issquareof316issquareof425issquareof5如果一个数不是任何整数的平方,则该数被称为非平方数。例如,前15个非平方数是-2,3,5,6,

使用C++找到数组中唯一配对的数量使用C++找到数组中唯一配对的数量Sep 07, 2023 am 11:53 AM

我们需要适当的知识才能在C++的数组语法中创建几个唯一的对。在查找唯一对的数量时,我们计算给定数组中的所有唯一对,即可以形成所有可能的对,其中每个对应该是唯一的。例如-Input:array[]={5,5,9}Output:4Explanation:Thenumberofalluniquepairsare(5,5),(5,9),(9,5)and(9,9).Input:array[]={5,4,3,2,2}Output:16寻找解决方案的方法有两种方法可以解决这个问题,它们是&minus;

在C编程中,找到一个圆的面积在C编程中,找到一个圆的面积Aug 25, 2023 pm 10:57 PM

圆是封闭图形。圆上的所有点到圆内一点的距离都相等。中心点称为圆心。点到圆心的距离称为半径。面积是封闭图形尺寸跨度的定量表示。圆的面积是圆的尺寸内包围的面积。计算圆面积的公式,Area=&pi;*r*r为了计算面积,我们给出了圆的半径作为输入,我们将使用公式来计算面积,算法STEP1:Takeradiusasinputfromtheuserusingstdinput.STEP2:Calculatetheareaofcircleusing,&nbsp;&nbsp;area=(

使用C++编写的数组右旋转的反转算法使用C++编写的数组右旋转的反转算法Sep 08, 2023 pm 08:17 PM

在本文中,我们将了解逆转算法,将给定的数组向右旋转k个元素,例如&minus;Input:arr[]={4,6,2,6,43,7,3,7},k=4Output:{43,7,3,7,4,6,2,6}Explanation:Rotatingeachelementofarrayby4-elementtotherightgives{43,7,3,7,4,6,2,6}.Input:arr[]={8,5,8,2,1,4,9,3},k=3Output:{4,9,3,8,5,8,2,1}寻找解决方案的方

使用C++编写代码,找到具有相同最小值和最大值的子数组的数量使用C++编写代码,找到具有相同最小值和最大值的子数组的数量Aug 25, 2023 pm 11:33 PM

在本文中,我们将使用C++解决寻找最大值和最小值相同的子数组数量的问题。以下是该问题的示例&minus;Input:array={2,3,6,6,2,4,4,4}Output:12Explanation:{2},{3},{6},{6},{2},{4},{4},{4},{6,6},{4,4},{4,4}and{4,4,4}arethesubarrayswhichcanbeformedwithmaximumandminimumelementsame.Input:array={3,3,1,5,

使用C++按给定大小将双向链表分组反转使用C++按给定大小将双向链表分组反转Sep 04, 2023 am 09:49 AM

在这个问题中,我们得到一个指向链表头部的指针和一个整数k。在大小为k的组中,我们需要反转链表。例如-Input:1<->2<->3<->4<->5(doublylinkedlist),k=3Output:3<->2<->1<->5<->4寻找解决方案的方法在这个问题中,我们将制定一个递归算法来解决这个问题。在这种方法中,我们将使用递归并使用递归来解决问题。示例#include<iostream&

使用C++编写的数组旋转的逆转算法使用C++编写的数组旋转的逆转算法Aug 28, 2023 pm 11:13 PM

在给定的问题中,我们有一个数组,并且我们需要使用反转算法将数组旋转d个元素,例如&minus;Input:arr[]=[1,2,3,4,5,6,7],d=2Output:arr[]=[3,4,5,6,7,1,2]Explanation:Asyoucanseewehavetorotatethisarraybyd=2butourmaintaskistoachievethisbyusingareversaltechnique.我们对数组的旋转进行了一些反转技术的计算,并得出结论:首先,我们反转

使用C++编写,找到一个集合上的自反关系的数量使用C++编写,找到一个集合上的自反关系的数量Aug 26, 2023 pm 08:17 PM

在本文中,我们将解释在一个集合上找到反身关系的方法。在这个问题中,我们给出一个数字n,以及一个由n个自然数组成的集合,我们必须确定反身关系的数量。反身关系-如果对于集合A中的每个'a',(a,a)属于关系R,则称关系R是集合A上的反身关系。例如-Input:x=1Output:1Explanation:set={1},reflexiverelationsonA*A:{{1}}Input:x=2Output:4Explanation:set={1,2},reflexiverelationsonA*

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft