Problem Type : Number Theory
#include<stdio.h>
#include<math.h>
unsigned long int happy (unsigned long int a)
{
long int n=0,m,i;
if(a==1)
return a;
else if( a<=9)
return a;
else if(a>9)
{
for(i=0;; i++)
{
n += pow((a%10),2);
m=a/10;
a=m;
if(a<=9)
{
a=n+m*m;
break;
}
}
return happy(a);
}
}
int main()
{
int i;
unsigned long int n,l,a,sum;
scanf("%lu",&n);
for(i=1; i<=n; i++)
{
scanf("%lu",&l);
if(l<=9){
a=l*l;
}
else
a=l;
sum=happy(a);
if(sum==1)
{
printf("Case #%d: %lu is a Happy number.\n",i,l);
}
else
printf("Case #%d: %lu is an Unhappy number.\n",i,l);
}
return 0;
}
#include<stdio.h>
#include<math.h>
unsigned long int happy (unsigned long int a)
{
long int n=0,m,i;
if(a==1)
return a;
else if( a<=9)
return a;
else if(a>9)
{
for(i=0;; i++)
{
n += pow((a%10),2);
m=a/10;
a=m;
if(a<=9)
{
a=n+m*m;
break;
}
}
return happy(a);
}
}
int main()
{
int i;
unsigned long int n,l,a,sum;
scanf("%lu",&n);
for(i=1; i<=n; i++)
{
scanf("%lu",&l);
if(l<=9){
a=l*l;
}
else
a=l;
sum=happy(a);
if(sum==1)
{
printf("Case #%d: %lu is a Happy number.\n",i,l);
}
else
printf("Case #%d: %lu is an Unhappy number.\n",i,l);
}
return 0;
}
No comments:
Post a Comment