计算数组元素总和 输入n个整数到数组中,然后计算数组中n个整数的和并输出。其中1≤n≤1000
展开全部
#include "stdio.h"
int main(int argc,char *argv[]){
int a[1000],sum,i,n;
printf("Please enter n(int 0<n<1001)...\n");
if(scanf("%d",&n)!=1 || n<1 || n>1000){
printf("Input error, exit...\n");
return 0;
}
printf("Please enter %d integer(s)...\n",n);
for(sum=i=0;i<n;scanf("%d",a+i),sum+=a[i++]);
printf("The sum of the %d integer(s) is %d\n",n,sum);
return 0;
}
运行样例:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询