龙空技术网

C++实现等差数列求和、求末项、求项数

Joephy 16

前言:

当前你们对“c语言数列求和3”都比较注意,姐妹们都想要学习一些“c语言数列求和3”的相关资讯。那么小编也在网摘上网罗了一些关于“c语言数列求和3””的相关文章,希望大家能喜欢,你们一起来了解一下吧!

求和:

#include <bits/stdc++.h>

using namespace std;

int main ()

{

int shouxiang,moxiang,xiangshu,qiuhe;

cout<<"请输入首项:";

cin>>shouxiang;

cout<<"请输入末项:";

cin>>moxiang;

cout<<"请输入项数:";

cin>>xiangshu;

qiuhe = (shouxiang+moxiang)*xiangshu/2;

cout<<shouxiang<<"加到"<<moxiang<<"的和为:"<<qiuhe;

return 0;

}

求末项:

#include <bits/stdc++.h>

using namespace std;

int main ()

{

int shouxiang,moxiang,gongcha,xiangshu,qiuhe;

cout<<"请输入首项:";

cin>>shouxiang;

cout<<"请输入项数:";

cin>>xiangshu;

cout<<"请输入公差:";

cin>>gongcha;

moxiang=shouxiang+(xiangshu-1)*gongcha;

cout<<"末项为:"<<moxiang;

return 0;

}

求项数:

#include <bits/stdc++.h>

using namespace std;

int main ()

{

int shouxiang,moxiang,gongcha,xiangshu;

cout<<"请输入首项:";

cin>>shouxiang;

cout<<"请输入末项:";

cin>>moxiang;

cout<<"请输入公差:";

cin>>gongcha;

xiangshu = (moxiang-shouxiang)/gongcha+1;

cout<<shouxiang<<"到"<<moxiang<<"的项数为:"<<xiangshu;

return 0;

}

附儿子默写的等差数列公式,不忍直视!

标签: #c语言数列求和3 #子序列求和c语言