Advertisement
JosepRivaille

P96965: Reducció de dígits

Apr 4th, 2015
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.43 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int suma_digits(int x) {
  6.     int suma = 0;
  7.     while (x > 0) {
  8.         suma = suma + x%10;
  9.         x = x/10;
  10.     }
  11.     return suma;
  12. }
  13.  
  14. int reduccio_digits(int x) {
  15.     int suma = suma_digits(x);
  16.     if (suma >= 10) return reduccio_digits(suma);
  17.     else return suma;
  18. }
  19.  
  20. //Pre: Llegeix nombre x
  21. //Post: Obtenim la reducció d'aquest
  22. int main() {
  23.     int x;
  24.     cin >> x;
  25.     cout << reduccio_digits(x) << endl;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement