forked from codehouseindia/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHappyNumber.java
More file actions
27 lines (23 loc) · 717 Bytes
/
HappyNumber.java
File metadata and controls
27 lines (23 loc) · 717 Bytes
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
public class HappyNumber
{
public static int isHappyNumber(int num){
int rem = 0, sum = 0;
while(num > 0){
rem = num%10;
sum = sum + (rem*rem);
num = num/10;
}
return sum;
}
public static void main(String[] args) {
int num = 82;
int result = num;
while(result != 1 && result != 4){
result = isHappyNumber(result);
}
if(result == 1)
System.out.println(num + " is a happy number");
else if(result == 4)
System.out.println(num + " is not a happy number");
}
}