Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int n,k, sumaNumeros, numeroFeliz, contador, resto;
  9.  
  10.     do {
  11.         cout << "Introduce el numero a comprobar si es feliz: "; cin >> n;
  12.         cout << "Introduce el grado: "; cin >> k;
  13.         cout << endl;
  14.     } while (n < 2 || k < 1);
  15.  
  16.     numeroFeliz = n;
  17.     for (int i=0; i<k; i++) {
  18.  
  19.         sumaNumeros=0;
  20.         resto=numeroFeliz;
  21.         while(resto>0){
  22.             sumaNumeros=sumaNumeros+pow(resto%10, 2);
  23.             resto=resto/10;
  24.         }
  25.  
  26.         numeroFeliz=sumaNumeros;
  27.     }
  28.  
  29.     if(numeroFeliz==1) {
  30.         cout << n << " es un numero feliz" << endl;
  31.     } else {
  32.         cout << n << " NO es un numero feliz" << endl;
  33.     }
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement