btcq.net
当前位置:首页 >> C语言等差数列求和whilE >>

C语言等差数列求和whilE

新手做的,菜鸟级别#include <stdio.h> int main(){ int a,d,n,i = 1; printf("enter the first number:"); scanf("%d",&a); printf("enter the space:"); scanf("%d",&d); printf("enter the step number:"); scanf("%d",&n); int C = a,an = 0; while(i < n

#include <stdio.h> int main(void) { int i = 1; int sum = 0; while(i <= 100) { sum = sum + i; i=i+3; } printf("1+4+7++100 = %d\n",sum); }

你这公式本来就是计算末项的啊.计算和应该是这样: #include<stdio.h> main(){int a,d,n,i,s,an;scanf("%d%d%d",&a,&d,&n);an=0; s=0;for (i=1;i<=n;i=i++,a=a+d);{an=a+an; s=s+an;printf("%d",s);}} 我那程序有点问题,正确的应该是: #include<stdio.h> main() { int a,d,n,i,s; scanf("%d%d%d",&a,&d,&n); s=0; for(i=1;i<=n;i++,a=a+d) { s=s+a; printf("%d\n",s); } }

/*求等差数列的和*/#include<stdio.h> int sum(int start,int dk,int num){ int i=0; int temp=0; int end=start; for(i;i<num;++i){ temp=temp+end; end=end+dk; } return temp; }//sum() int main() { int start; int dk; int num; printf("输入首项 start= \n"); scanf(

#include int main(void){ int n, num, sum=0; printf("please input the n:\n"); scanf("%d", &n); num = 2*n - 1; while(num >= 1) { sum += num; num -= 2; } printf("sum is %d\n", sum); return 0;}

#includeint main(){ int n,i=1; printf("请输入等差数列首项:"); scanf("%d",&n); do{ printf("第%2d项是:%2d\n",i,n); n+=3; i++; }while(i 评论0 0 0

你写的这个公式只能求首项为1,公差为1的等差数列的前n项和.对于一般等差数列,这个公式是求不了的.代码如下:#include <stdio.h> void main() { int n; printf("请输入等差数列的项数n: "); scanf("%d",&n); printf("%d",n*(n+1)/2); }

#include <stdio.h>#include <stdlib.h> int main() { float i=1,n,sum=0; printf("please input n:\n"); scanf("%f",&n); while(i<=n) { sum=sum+1/i; i++; } printf("sum=%f\n",sum); return 0; }

参考代码如下:#include<stdio.h> int main() { int N,i,s; scanf("%d",&N); for(i=1;i<=N;++i) printf("%d ",4*i-3); s=N+4/2*N*(N-1); printf("\nS=%d",s); return 0; }

网站首页 | 网站地图
All rights reserved Powered by www.btcq.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com