1 //求任意一个整数各个数位上的数字之和 2 //by feifei435 3 #include <iostream.h> 4 5 //函数sum返回值为各位数的和 6 int sum(int num) 7 { 8 int temp=0; 9 while (num != 0) 10 { 11 temp += num%10; 12 num=num/10; 13 } 14 return temp; 15 16 } 17 18 int main() 19 { 20 int number=0; 21 cout<<"输入要求各位之和的数(0~2^31)"<<endl; 22 cin>>number; 23 cout<<sum(number)<<endl; 24 return 0; 25 }