GeeksforGeeks Solution For Hard Domain .Below You Can Find The Solution Of School Basic ,Easy ,Medium . Or Hackerrank Solution You Can Also Direct Submit Your Solution to Geeksforgeeks Same Problem .You Need to login then you can submit you answers
Problem :- Happy Number
Submit Your Solution :- Click Here
Solution :-
#include <bits/stdc++.h>
using namespace std;
int numSquareSum(int n)
{
int squareSum = 0;
while (n)
{
squareSum += (n % 10) * (n % 10);
n /= 10;
}
return squareSum;
}
bool isHappynumber(int n)
{
int slow, fast;
slow = fast = n;
do
{
slow = numSquareSum(slow);
fast = numSquareSum(numSquareSum(fast));
}
while (slow != fast);
return (slow == 1);
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
if (isHappynumber(n))
cout <<"1\n";
else
cout <<"0\n";
}
return 0;
}
Output:-
0 Comments: