This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Author : Md.Mehadi Hasan menon. | |
* Problem : 10591 - Happy Number. | |
* Status : Accepted. | |
* Date : 13.10.2014. | |
**/ | |
#include <stdio.h> | |
int happyNumber(unsigned int num); | |
int main() | |
{ | |
unsigned int i, n, tc, a; | |
scanf("%u", &tc); | |
for(i = 1; i <= tc; i++) | |
{ | |
scanf("%u", &n); | |
a = happyNumber(n); | |
if(a == 1) | |
printf("Case #%u: %u is a Happy number.\n", i, n); | |
else | |
printf("Case #%u: %u is an Unhappy number.\n", i, n); | |
} | |
return 0; | |
} | |
int happyNumber(unsigned int num) | |
{ | |
int digit, sum; | |
sum = 0; | |
while(num != 0) | |
{ | |
digit = num % 10; | |
sum = sum + (digit * digit); | |
num = num / 10; | |
} | |
if(sum >= 10) | |
happyNumber(sum); | |
else | |
return sum; | |
} |
Comments
Post a Comment