-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHappy.java
executable file
·49 lines (43 loc) · 1.04 KB
/
Happy.java
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
import java.util.*;
public class Happy
{
static int digits(String d)
{
int nod=d.length();
return nod;
}
static int sumDigits(int n1)
{
int sum1=0;
while(n1>0)
{
int a =n1%10;
sum1 = sum1+(a*a);
n1=n1/10;
}
return sum1;
}
public static void main(String args [])
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number");
int n = scan.nextInt();
String ns = String.valueOf(n);
int sum =0;
int digits;
digits = Happy.digits(ns);
while(digits>1 )
{
sum = Happy.sumDigits(n);
System.out.println(sum);
ns=String.valueOf(sum);
n=sum;
digits=Happy.digits(ns);
}
if(sum==1)
{
System.out.println("It's a happy number");
}
else
System.out.println("It's not a happy number");
}}