This program checks whether a given number is a Happy Number. It repeatedly replaces the
number with the sum of the squares of its digits until it either becomes 1 (happy) or enters a cycle
that never reaches 1 (unhappy). For example: 1912+92 = 8282 + 2² = 68 → 62 +8² = 100
→12+02+021 → Happy.
->
1.
class happyNumber {
2.
3.
4.
5.
6.
7.
8.
public static void main(String[] args) {
Scanner sc
System.out.print("Enter a number to check: ");
int num sc.nextInt();
if (isHappy (num)) {
Scanner(System.in);
9.
System.out.println(num + " is a Happy Number.");
10.
} else {
11.
System.out.println(num + " is NOT a Happy Number.");
12.
}
13.
}
14.
15.
16.
17.
18.
19.
20.
21.
22.
public static boolean isHappy (int n) {
int slow = n;
int fast sumOfSquares (n);
while (fast != 1 && slow != fast) {
}
slow sumofSquares (slow);
fast sumofSquares (sumOfSquares (fast));
Zoom
+ 100%
Close