c++【入门】求任意三位数各个数位上数字的和说明对于一个任意的三位自然数X编程计算其各个数位上的数字之和S。输入格式输入一行只有一个整数x100=x=999 输出格式输出只有一行包括1个整数
#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;
}
原文地址: http://www.cveoy.top/t/topic/h5pC 著作权归作者所有。请勿转载和采集!