-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
happy_number.cpp
39 lines (35 loc) · 924 Bytes
/
happy_number.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
/**
* @file
* @brief A happy number is a number whose sum of digits is calculated until the
* sum is a single digit, and this sum turns out to be 1
*/
#include <iostream>
/**
* Checks if a decimal number is a happy number
* \returns true if happy else false
*/
template <typename T>
bool is_happy(T n) {
T s = 0; // stores sum of digits
while (n > 9) { // while number is > 9, there are more than 1 digit
while (n != 0) { // get digit
T d = n % 10;
s += d;
n /= 10;
}
n = s;
s = 0;
}
return (n == 1) ? true : false; // true if k == 1
}
/** Main function */
int main() {
int n;
std::cout << "Enter a number:";
std::cin >> n;
if (is_happy(n))
std::cout << n << " is a happy number" << std::endl;
else
std::cout << n << " is not a happy number" << std::endl;
return 0;
}