C语言程序计算整数各位数字之和

2025-09-26 19:09:43

1、ubuntu 14.04 linux c

gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2

2、#include<stdio.h>

#include<stdlib.h>

int main()

{

        char integer[100];

        int value = 0,temp_value = 0,sum = 0;

        gets(integer);

        value = atoi(integer);

        temp_value = value;

        while(value >= 1)

        {

                sum += value%10;

                value = value / 10;

        }

        printf("the integer is %d and the sum of decimal digits is %d \n",temp_value,sum);

        return 0;

}

3、xxx@linux:~/code$ gcc -o sum sum.c 

xxx@linux:~/code$ ./sum 

123

the integer is 123 and the sum of decimal digits is 6 

xxx@linux:~/code$ ./sum

12345

the integer is 12345 and the sum of decimal digits is 15

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
相关推荐
  • 阅读量:75
  • 阅读量:39
  • 阅读量:70
  • 阅读量:86
  • 阅读量:48
  • 猜你喜欢