bool isHappy(int n){
int o = n;
while(n != 1){
if(n == 4) break;//back to nonendless
//printf("%d\n" , n);
int tmp = 0;
while(n){
tmp += (n % 10) * (n % 10);
n /= 10;
}
n = tmp;
if (o == tmp) break;//back to self
}
return n == 1;
}
I have found 4 is endless


@ManuelP
I am so sorry about it , code should be :bool isHappy(int n){ int o = n; while(n != 1){ if(n == 4) break;//back to nonendless //printf("%d\n" , n); int tmp = 0; while(n){ tmp += (n % 10) * (n % 10); n /= 10; } n = tmp; if (o == tmp) break;//back to self } return n == 1; }