Examples
- Java Program to Count Number of Digits in Number (Integer Length)
- Java Program to Reverse a String (4 Methods)
- How to Reverse an Array in Java? Array Reverse Program
- How to Find Power of a Number in Java? 4 Programs
- How to Find Factors of a Number in Java? Factors Program
- Check Disarium Number in Java (3 Easy Programs)
- Find Keith Number in Java (Easy Programs)
- Happy Number in Java (Easy Programs With Logic)
- Find Sunny Number in Java (Easy Programs)
Happy Number in Java (Easy Programs With Logic)
A happy number is a special type of number that, when you repeatedly square its digits and add them together, eventually equals 1. If this process doesn't reach 1 and instead goes into an endless loop, the number is not a happy number.
Let us understand it with an example using the number 19:
-
Start with the number 19.
-
Square each digit and add them together: 1^2 + 9^2 = 1 + 81 = 82.
Now, repeat the process with the result, 82:
-
Square each digit and add them together: 8^2 + 2^2 = 64 + 4 = 68.
-
Repeat again: 6^2 + 8^2 = 36 + 64 = 100.
-
Once more: 1^2 + 0^2 + 0^2 = 1 + 0 + 0 = 1.
After a few iterations, we reach 1, so 19 is a happy number.
Not all numbers are happy numbers.
For example, if we start with 4:
-
Start with the number 4.
-
Square each digit and add them together: 4^2 = 16.
-
Repeat: 1^2 + 6^2 = 1 + 36 = 37.
-
Repeat: 3^2 + 7^2 = 9 + 49 = 58.
-
Repeat: 5^2 + 8^2 = 25 + 64 = 89.
-
Repeat: 8^2 + 9^2 = 64 + 81 = 145.
-
Repeat: 1^2 + 4^2 + 5^2 = 1 + 16 + 25 = 42.
-
Repeat: 4^2 + 2^2 = 16 + 4 = 20.
-
Repeat: 2^2 + 0^2 = 4 + 0 = 4.
The process keeps cycling between 4 and 20, never reaching 1. So, 4 is not a happy number.
In this tutorial, we will learn how to check Happy Number in Java using different approaches, methods, and examples.
Happy Number Program in Java Using while loop and HashSet
Here's how to check for Happy Numbers in Java using the HashSet method along with the while loop:
Code
import java.util.HashSet;
public class HappyNumberChecker {
public static boolean isHappy(int n) {
HashSet<Integer> seen = new HashSet<>();
while (n != 1 && !seen.contains(n)) {
seen.add(n);
n = getNextNumber(n);
}
return n == 1;
}
public static int getNextNumber(int n) {
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += digit * digit;
n /= 10;
}
return sum;
}
public static void main(String[] args) {
int number = 19;
boolean result = isHappy(number);
if (result) {
System.out.println(number + " is a Happy Number.");
} else {
System.out.println(number + " is not a Happy Number.");
}
}
}
Output
19 is a Happy Number.
Explanation
-
We define an isHappy method that takes an integer n as input and returns true if it's a Happy Number, or false otherwise.
-
Inside the isHappy method, we use a HashSet called seen to store the numbers we've seen during the process.
-
We enter a while loop that continues until n becomes 1 (a Happy Number) or we encounter a number that we've seen before.
-
In each iteration, we add the current value of n to the seen HashSet and update n to the sum of the squares of its digits using the getNextNumber method.
-
The getNextNumber method calculates the sum of the squares of the digits of a number.
-
Finally, in the main method, we test the isHappy method with the number 19 and print the result, which indicates that 19 is indeed a Happy Number.
Check Happy Number in Java Using Floyd Algorithm
Here's how to check for Happy Number in Java using the Floyd's Cycle Detection Algorithm (Tortoise and Hare method):
Code
public class HappyNumberChecker {
public static boolean isHappy(int n) {
int slow = n;
int fast = n;
do {
slow = getNextNumber(slow);
fast = getNextNumber(getNextNumber(fast));
} while (slow != fast);
return slow == 1;
}
public static int getNextNumber(int n) {
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += digit * digit;
n /= 10;
}
return sum;
}
public static void main(String[] args) {
int number = 70;
boolean result = isHappy(number);
if (result) {
System.out.println(number + " is a Happy Number.");
} else {
System.out.println(number + " is not a Happy Number.");
}
}
}
Output
70 is a Happy Number.
Explanation
-
We define an isHappy method that takes an integer n as input and returns true if it's a Happy Number, or false otherwise.
-
Inside the isHappy method, we initialize two pointers, slow and fast, both starting at the value of n.
-
We enter a loop that uses the slow pointer to calculate the next number using the getNextNumber method and the fast pointer to calculate the next number twice. This is the essence of the Floyd's Cycle Detection Algorithm.
-
If there is a cycle in the sequence of numbers, the slow and fast pointers will eventually meet at the same value.
-
If the loop exits and slow becomes 1, we return true because the number is a Happy Number. Otherwise, we return false.
-
The getNextNumber method calculates the sum of the squares of the digits of a number.
-
In the main method, we test the isHappy method with the number 70 and print the result, which indicates that 70 is indeed a Happy Number.
Happy Number Program in Java Using Recursion
Here's how to check for Happy Numbers in Java using recursion:
Code
public class HappyNumberChecker {
public static boolean isHappy(int n) {
if (n == 1 || n == 7) {
return true;
} else if (n < 10) {
return false;
} else {
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += digit * digit;
n /= 10;
}
return isHappy(sum);
}
}
public static void main(String[] args) {
int number = 19;
boolean result = isHappy(number);
if (result) {
System.out.println(number + " is a Happy Number.");
} else {
System.out.println(number + " is not a Happy Number.");
}
}
}
Output
19 is a Happy Number.
Explanation
-
We define an isHappy method that takes an integer n as input and returns true if it's a Happy Number, or false otherwise.
-
We first check for base cases: if n is 1 or 7, we return true because they are Happy Numbers, and if n is less than 10, we return false because single-digit numbers that are not 1 or 7 are not Happy Numbers.
-
If n is not a base case, we enter an else block where we calculate the sum of the squares of its digits using a while loop.
-
We then recursively call the isHappy method with the calculated sum as the new input.
-
This process continues until we either reach a base case (1 or 7) and return true or get stuck in a loop and return false.
-
In the main method, we test the isHappy method with the number 19 and print the result, which indicates that 19 is indeed a Happy Number.
Learn Next: