fork download
  1. //实验3.4
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main() {
  6. int a;
  7. cout << "请输入一个整数: ";
  8. cin >> a;
  9.  
  10. int b = 0;
  11. while (a!= 0) {
  12. b += a % 10;
  13. a /= 10;
  14. }
  15.  
  16. cout << "各位数字之和为: " << b << endl;
  17. return 0;
  18. }
  19.  
Success #stdin #stdout 0.01s 5288KB
stdin
358
stdout
请输入一个整数: 各位数字之和为: 16