7/11/2013

Summing digits - UVA 11332

  1. #include<iostream>
  2. using namespace std;
  3. long g(long x)
  4. {
  5.         if(x<10){return x;}
  6.         long sum = 0;
  7.         long temp = x;
  8.        
  9.         while(temp>0){
  10.                 sum+=temp%10;
  11.                 temp/=10;
  12.         }
  13.         return g(sum);
  14. }
  15. int main ()
  16. {
  17.         long num , result;
  18.         while(cin>>num)
  19.         {
  20.                 if(num==0)
  21.                         break;
  22.                 else{
  23.                         result = g(num);
  24.                         cout<<result<<endl;
  25.                 }
  26.         }
  27.         return 0;
  28. }

You can find more of my solutions on Here

No comments:

Post a Comment