算术级数和的C ++程序
给定“a”(第一项),“d”(共同差)和“n”(字符串中的值数),任务是生成序列并由此计算其和。
什么是算术级数
算术级数是具有相同差的数字序列,其中系列的第一项固定为'a',而它们之间的公共差为'd'。
它表示为-
a,a+d,a+2d,a+3d,。。
示例
Input-: a = 1.5, d = 0.5, n=10 Output-: sum of series A.P is : 37.5 Input : a = 2.5, d = 1.5, n = 20 Output : sum of series A.P is : 335
下面使用的方法如下-
输入数据作为第一项(a),共同差(d)和系列中的项数(n)
遍历循环直到n,并继续将第一个项添加到具有差异的临时变量中
打印结果输出
算法
Start
Step 1-> declare Function to find sum of series
float sum(float a, float d, int n)
set float sum = 0
Loop For int i=0 and i<n and i++
Set sum = sum + a
Set a = a + d
End
return sum
Step 2-> In main() Set int n = 10
Set float a = 1.5, d = 0.5
Call sum(a, d, n)
Stop示例
#include<bits/stdc++.h>
using namespace std;
//查找序列和的功能。
float sum(float a, float d, int n) {
float sum = 0;
for (int i=0;i<n;i++) {
sum = sum + a;
a = a + d;
}
return sum;
}
int main() {
int n = 10;
float a = 1.5, d = 0.5;
cout<<"sum of series A.P is : "<<sum(a, d, n);
return 0;
}输出结果
sum of series A.P is : 37.5