C语言求等差数列的和的程序

2024-11-01 11:44:15
推荐回答(3个)
回答1:

/*求等差数列的和*/
#include
int sum(int start,int dk,int num){
int i=0;
int temp=0;
int end=start;
for(i;i temp=temp+end;
end=end+dk;
}
return temp;
}//sum()
int main()
{
int start;
int dk;
int num;
printf("输入首项 start= \n");
scanf("%d",&start);
printf("输入公差 dk= \n");
scanf("%d",&dk);
printf("输入项数 num= \n");
scanf("%d",&num);
int summation=sum(start,dk,num);
printf("和是 ");
printf("%d",summation);
printf("\n");
return;
}//main()

回答2:

#include
void main(){
long first,last,count,step;
scanf("%l%l%l%l",&first,&last,&count,&step);
printf("sum=%l",((first+last)*count)/(2*step));
}

回答3:

step不能为0