50. Happy Number
17 Jul 2020 | Daily Algorithms
class Solution {
public:
bool isHappy(int n) {
while(true)
{
int sum = 0;
while(n!=0)
{
int lastDigit = n % 10;
sum += lastDigit*lastDigit;
n = n/10;
}
if(1 <= sum && sum <= 9)
{
if(sum == 1 || sum == 7)
{
return true;
}
else
{
return false;
}
}
else
{
n = sum;
}
}
}
};
class Solution {
public:
bool isHappy(int n) {
while(true)
{
int sum = 0;
while(n!=0)
{
int lastDigit = n % 10;
sum += lastDigit*lastDigit;
n = n/10;
}
if(1 <= sum && sum <= 9)
{
if(sum == 1 || sum == 7)
{
return true;
}
else
{
return false;
}
}
else
{
n = sum;
}
}
}
};
Comments