c語言中如何調用函數求數列和?
#include<stdio.h>
int main(void){
int a1,a2,d; //a1:首項;a2:末項;d:公差
int sum =1; //sum:等差數列和;n:項數
int n = 1;
scanf("%d%d%d",&a1,&a2,&d);
n = (a2-a1)/d+1; //項數=(末項-首項)÷公差+1
sum = n*(a1+a2)/2; //和=(首項+末項)×項數÷2
printf("%d\n",sum);
return 0;
}