C 语言实例 - 计算数组元素之和
使用 for 循环迭代出输出元素,并将各个元素相加:
实例
#include <stdio.h>
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int sum, loop;
sum = 0;
for(loop = 9; loop >= 0; loop--) {
sum = sum + array[loop];
}
printf("元素和为:%d", sum);
return 0;
}
#include <stdio.h>
int main() {
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int sum, loop;
sum = 0;
for(loop = 9; loop >= 0; loop--) {
sum = sum + array[loop];
}
printf("元素和为:%d", sum);
return 0;
}
输出结果为:
元素和为:45