-
Notifications
You must be signed in to change notification settings - Fork 2
/
Happy Number.cpp
70 lines (66 loc) · 1.36 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
This Program Check Weather the input(integer) number is Happy or Not.Also Print Happy Numbers Upto Input number.
If the repeated sum of squares of the digits of a number is equal to 1, it is considered to be Happy.
For Example:
23 is a happy number, as:
2 ^ 2 + 3 ^ 2 = 13
1 ^ 2 + 3 ^ 2 = 10
1 ^ 2 + 0 ^ 2 = 1
Sequence of happy numbers: 1, 7, 10, 13, 19, 23, ...
*/
#include <iostream>
#include <iomanip>
using namespace std;
void Check_Happy_Number(int num)
{
int sum=1,n,r;
n=num;
while(n!=1 || n!=4)
{
sum=0;
while(n!=0){
r=n%10;
sum=sum+r*r;
n=n/10;
}
if(sum==1 || sum==4)
break;
n=sum;
}
if(sum==1)
cout<<"👍 "<<num<<" is a 😃Happy Number.\n\n";
else
cout<<"❌ "<<num<<" is a ☹️UnHappy Number.\n\n";
}
void Print_Happy_Number(int upto)
{
int i,sum=1,n;
cout<<"😃Happy Numbers Upto "<<upto<<" Are:"<<endl<<endl;
for(i=1;i<=upto;i++)
{
n=i;
while(n!=1 || n!=4)
{
sum=0;
while(n!=0){
sum=sum+(n%10)*(n%10);
n=n/10;
}
if(sum==1 || sum==4)
break;
n=sum;
}
if(sum==1)
cout<<setw(4)<<i;
}
}
int main()
{
int num;
//cout<<"Enter a Number : ";
cin>>num;
cout<<"Entered Number : "<<num<<endl<<endl;
Check_Happy_Number(num);
Print_Happy_Number(num);
return 0;
}