Advertisement
DatProgrammer

P96965

Oct 19th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.44 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int sum_digits(int n) {
  5.  
  6.   int r = 0;
  7.   while (n > 0) {
  8.    
  9.     r = r + (n%10);
  10.     n = n/10;
  11.   }
  12.   if (r < 10) return r;
  13.   else return sum_digits(r);
  14. }
  15.  
  16. int reduction_of_digits(int x) {
  17.  
  18.   if (x < 10) return x;
  19.   else {
  20.    
  21.     int z = sum_digits(x);
  22.     return z;
  23.   }
  24. }
  25.  
  26. int main() {
  27.  
  28.   int x;
  29.   while (cin >> x) {
  30.    
  31.     cout << reduction_of_digits(x) << endl;
  32.   }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement