Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- int main()
- {
- int n,k, sumaNumeros, numeroFeliz, contador, resto;
- do {
- cout << "Introduce el numero a comprobar si es feliz: "; cin >> n;
- cout << "Introduce el grado: "; cin >> k;
- cout << endl;
- } while (n < 2 || k < 1);
- numeroFeliz = n;
- for (int i=0; i<k; i++) {
- sumaNumeros=0;
- resto=numeroFeliz;
- while(resto>0){
- sumaNumeros=sumaNumeros+pow(resto%10, 2);
- resto=resto/10;
- }
- numeroFeliz=sumaNumeros;
- }
- if(numeroFeliz==1) {
- cout << n << " es un numero feliz" << endl;
- } else {
- cout << n << " NO es un numero feliz" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement