-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.cpp
42 lines (33 loc) · 844 Bytes
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <cstdio>
using namespace std;
int get_digit_squared_sum(int n) {
int sum = 0;
while(n != 0) {
sum = sum + ((n % 10) * (n % 10));
n = n / 10;
}
return sum;
}
int main()
{
int testCase, num, a;
while(scanf("%d", &testCase)==1) {
for(int i = 1; i <= testCase; i++) {
cin >> num;
a = num;
while(1) {
if(get_digit_squared_sum(a) == 1) {
printf("Case #%d: %d is a Happy number.\n", i, num);
break;
} else if(get_digit_squared_sum(a) == 4 || get_digit_squared_sum(a) == 0) {
printf("Case #%d: %d is an Unhappy number.\n", i, num);
break;
} else {
a = get_digit_squared_sum(a);
}
}
}
}
return 0;
}