## Sabre Holdings Interview Question

Java Developers**Country:**India

something like this should be ok

```
public static void main(String[] args) {
var input = new Scanner(System.in);
System.out.print("Enter number <0 to exit> : ");
var n = input.nextInt();
while (n != 0) {
System.out.println(isHappy(n) ? 1 : 0);
System.out.print("Enter number <0 to exit> : ");
n = input.nextInt();
}
}
private static boolean isHappy(int n) {
var set = new HashSet<Integer>();
int computed;
do {
computed = 0;
set.add(n);
while (n > 0) {
computed += square(n % 10);
n /= 10;
}
if (set.contains(computed)) break;
n = computed;
} while (computed != 1);
return computed == 1;
}
private static int square(int i) { return i * i; }
```

class HappyNumber {

static int numSquareSum(int n) {

int squareSum = 0;

while (n!= 0) {

squareSum += (n % 10) * (n % 10);

n /= 10;

}

return squareSum;

}

// method return true if n is Happy number

static boolean isHappynumber(int n) {

int slow, fast;

slow = fast = n;

do {

slow = numSquareSum(slow);

fast = numSquareSum(numSquareSum(fast));

} while (slow != fast);

return (slow == 1);

}

public static void main(String[] args) {

int n = 13;

if (isHappynumber(n))

System.out.println(n + " is a Happy number");

else

System.out.println(n + " is not a Happy number");

}

}

LeetCode 202. Happy Number

- Sithis March 19, 2019