对于如何添加函数返回的所有值的和,有什么想法吗?
我需要将长整数的单个数字相加在一起。我已经知道了如何隔离和打印我需要的每一个数字,但我不知道该如何设置代码来返回这些数字,以便将它们相加在一起。
发布于 2022-10-24 21:31:19
#include <stdio.h>
int SumOfDigits(long x)
{
int sum;
for(sum=0; x; x/=10) sum += x%10;
return sum;
}
int main(void) {
long x = 12345678;
printf("Sum of Digits of %lu is %d\n", x, SumOfDigits(x));
return 0;
}输出
Success #stdin #stdout 0s 5420KB
Sum of Digits of 12345678 is 36因为:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36https://stackoverflow.com/questions/74186700
复制相似问题