SPOJ - Happy Numbers Problem Solution
Solution:-
using namespace std;
#define ll long long
ll cal_num(ll num)
{
int val;
ll sum=0;
while(num)
{
val=num%10;
sum+=val*val;
num/=10;
}
return sum;
}
int main()
{
ll num;
cin>>num;
int count=0;
while(1)
{
if(num==1)
{
cout<<count;
break;
}
else if(num<10 && num!=7)
{
cout<<"-1";
break;
}
num=cal_num(num);
count++;
}
return 0;
}
No comments:
Post a Comment