Thursday 6 August 2015

HPYNOS

Happy Numbers I

Link to the question : HPYNOS 

HINT :

Just simple brute force. Keep on breaking the number untill you get a single digit. If its 1 then its a happy number else not a happy number.

SOURCE CODE :

#include<stdio.h>



int main()
{
    char a[10];int i=0,b=0,x,c=1;
    gets(a);

    while(a[i]!='\0')
    {
            b= b + (a[i] - 48) * (a[i]-48);

            i++;
    }



    while(b>9)
    {     x=0;
        while(b>0)
        {    x=(b%10) * (b%10) +x;
             b=b/10;

        }
        b=x;
        c++;

    }
    if(b==1)
        printf("%d",c);
    else
        printf("-1");

    return 0;


}
 

No comments:

Post a Comment