C++ 编程:求三位数各位数字之和
#include
int main() { int x; cin >> x;
int sum = 0; // 初始化数字之和为0
// 计算个位上的数字
int onesDigit = x % 10;
sum += onesDigit;
// 计算十位上的数字
int tensDigit = (x / 10) % 10;
sum += tensDigit;
// 计算百位上的数字
int hundredsDigit = x / 100;
sum += hundredsDigit;
cout << sum << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/pOhA 著作权归作者所有。请勿转载和采集!