2017-06-04 18:27:09 +08:00
|
|
|
/* 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>
|
|
|
|
using namespace std;
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
int n,k,s=0,d;
|
|
|
|
cout << "Enter a number:";
|
|
|
|
cin >> n;
|
2017-09-29 21:26:47 +08:00
|
|
|
s=0;k=n;
|
2017-06-04 18:27:09 +08:00
|
|
|
while(k>9)
|
|
|
|
{
|
|
|
|
while(k!=0)
|
|
|
|
{
|
2017-09-29 21:26:47 +08:00
|
|
|
d=k%10;
|
2017-06-04 18:27:09 +08:00
|
|
|
s+=d;
|
2017-09-29 21:26:47 +08:00
|
|
|
k/=10;
|
2017-06-04 18:27:09 +08:00
|
|
|
}
|
|
|
|
k=s;
|
|
|
|
s=0;
|
|
|
|
}
|
|
|
|
if(k==1)
|
|
|
|
cout << n << " is a happy number" << endl;
|
|
|
|
else
|
|
|
|
cout << n << " is not a happy number" << endl;
|
|
|
|
}
|