假设我有一个 5 位整数
int num = 23456;
如何求其数字之和?
最佳答案
使用modulo operation获取最低有效位的值:
int num = 23456;
int total = 0;
while (num != 0) {
total += num % 10;
num /= 10;
}
如果输入可能是负数,那么最好检查一下并反转符号。
关于c - 找出数字的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58034764/