C 语言实例 – 计算数组元素之和

C语言教程1

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;
}

输出结果为:文章源自公式库网-https://www.gongshiku.com/html/202112/c-yuyanshili-jisuanshuzuyuansuzhihe.html

元素和为:45
文章源自公式库网-https://www.gongshiku.com/html/202112/c-yuyanshili-jisuanshuzuyuansuzhihe.html文章源自公式库网-https://www.gongshiku.com/html/202112/c-yuyanshili-jisuanshuzuyuansuzhihe.html
运营不易,
感谢支持!
weinxin
我的微信
我的微信公众号
我的微信公众号扫一扫
weinxin
我的公众号
 
公式库网
  • 本文由 公式库网 发表于 2021年12月7日20:31:55
  • 转载请务必保留本文链接:https://www.gongshiku.com/html/202112/c-yuyanshili-jisuanshuzuyuansuzhihe.html

发表评论