Skip to content

Instantly share code, notes, and snippets.

@shohan4556
Created April 30, 2015 02:22
Show Gist options
  • Save shohan4556/ee729c3b43b60904dfd7 to your computer and use it in GitHub Desktop.
Save shohan4556/ee729c3b43b60904dfd7 to your computer and use it in GitHub Desktop.
uva 10591 happy number solution in C
/// Author :Shohanur Rahaman
/// UVA 10591
#include<stdio.h>
#include<math.h>
int main()
{
int n,sum,a,tc,ca,b;
while(scanf("%d",&tc)==1){
for(ca=1;ca<=tc;ca++){
scanf("%d",&n);
a=n;
sum=0;
while(n<9 || n>9){
if(n<=9){
sum=n;
break;
}
while(n!=0){
b=n%10;
sum=sum+(b*b);
n=n/10;
}
n=sum;
sum=0;
}
if(sum==1 || sum==7)
printf("Case #%d: %d is a Happy number.\n",ca,a);
else
printf("Case #%d: %d is an Unhappy number.\n",ca,a);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment