class Solution {
public:
bool isHappy(int n)
{
string digits = to_string(n);
int total = 0;
for (int i=0; i<digits.size(); ++i)
{
total += ((char)digits[i]'0') * ((char)digits[i]'0');
}
if (1 == total)
return true;
else
return isHappy(total);
}
};
Why my solution does not work?

Happy numbers always converge to 1 and for the following iterations, the output will stay as 1.
Eg:
19 > 82 > 68 > 100 > 1 > 1 > 1 ...... stays at 1Unhappy numbers eventually enter a cycle which repeats.
Eg:
2 > 4 > 16 > 37 > 58 > 89 > 145 > 42 > 20 > 4 > 16 > 37 > 58 > 89 > 145 > 42 > 20 > 4Do you notice that the cycle repeats from 4 in the above example?

Happy numbers always converge to 1 and for the following iterations, the output will stay as 1. Eg: 19 > 82 > 68 > 100 > 1 > 1 > 1 ...... stays at 1
Unhappy numbers eventually enter a cycle which repeats. Eg: 2 > 4 > 16 > 37 > 58 > 89 > 145 > 42 > 20 > 4 > 16 > 37 > 58 > 89 > 145 > 42 > 20 > 4
Notice that the cycle repeats from 4 in the above example (an infinite loop).
In your case, the recursive call you make in the else condition  it never ends..