-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNext Happy Number.java
More file actions
40 lines (39 loc) · 834 Bytes
/
Next Happy Number.java
File metadata and controls
40 lines (39 loc) · 834 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
28
29
30
31
32
33
34
35
36
37
38
39
40
//https://practice.geeksforgeeks.org/problems/next-happy-number4538/1
class Solution{
static int nextHappy(int N){
// code here
for(int i=N+1;i<2147483646;i++){
if(check(i)==true){
return i;
}
}
return 0;
}
static boolean check(int n){
int k=10;
int a=n;
while(k!=0){
a=fact(a);
if(a/10==0){
if(a==1||a==7){
return true;
}
else{
return false;
}
}
k=a/10;
}
return false;
}
static int fact(int n){
int s=0;
int r=0;
while(n!=0){
r=n%10;
s=s+r*r;
n=n/10;
}
return s;
}
}