UVA 10591 Happy Number








#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long int n,a,b,i,j,s,m,r,t;
    cin>>t;
    for(i=1;i<=t;i++)
    {
    cin>>m;
    if(m<10)
    {
      n=m*m;
    }
    else
    {
        n=m;
    }

    while(n>9)
    {
        s=0;
        while(n!=0)
        {
            r=n%10;
            s=s+r*r;
            n=n/10;
        }
        n=s;
    }
    if(n==1)
    {
        printf("Case #%lld: %lld is a Happy number.\n",i,m);
    }
    else
    {
        printf("Case #%lld: %lld is an Unhappy number.\n",i,m);
    }
    }
    return 0;
}

Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment