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