Java Basic
Java Interview Programs
- Java Interview Programs
- Java Abundant Number
- Java Amicable Number
- Java Armstrong Number
- Java Average of N Numbers
- Java Automorphic Number
- Java Biggest of three numbers
- Java Binary to Decimal
- Java Common Divisors
- Java Composite Number
- Java Condense a Number
- Java Cube Number
- Java Decimal to Binary
- Java Decimal to Octal
- Java Disarium Number
- Java Even Number
- Java Evil Number
- Java Factorial of a Number
- Java Fibonacci Series
- Java GCD
- Java Happy Number
- Java Harshad Number
- Java LCM
- Java Leap Year
- Java Magic Number
- Java Matrix Addition
- Java Matrix Division
- Java Matrix Multiplication
- Java Matrix Subtraction
- JS Matrix Transpose
- Java Maximum Value of an Array
- Java Minimum Value of an Array
- Java Multiplication Table
- Java Natural Number
- Java Number Combination
- Java Odd Number
- Java Palindrome Number
- Java Pascalβs Triangle
- Java Perfect Number
- Java Perfect Square
- Java Power of 2
- Java Power of 3
- Java Pronic Number
- Java Prime Factor
- Java Prime Number
- Java Smith Number
- Java Strong Number
- Java Sum of Array
- Java Sum of Digits
- Java Swap Two Numbers
- Java Triangular Number
Java Program to Check Happy Number
Photo Credit to CodeToFun
π Introduction
In the domain of programming, certain mathematical properties and patterns are often explored. One such interesting concept is the notion of "happy numbers."
In this tutorial, we will delve into a Java program designed to determine whether a given number is a happy number.
π Example
Let's take a look at the Java code that checks whether a given number is a happy number or not.
public class HappyNumberChecker {
// Function to calculate the sum of squares of digits
static int sumOfSquares(int n) {
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += digit * digit;
n /= 10;
}
return sum;
}
// Function to check if a number is a happy number
static boolean isHappyNumber(int n) {
int slow = n, fast = n;
do {
slow = sumOfSquares(slow);
fast = sumOfSquares(sumOfSquares(fast));
} while (slow != fast);
return (slow == 1);
}
// Driver program
public static void main(String[] args) {
// Replace this value with your desired number
int number = 19;
// Check if the number is a happy number
if (isHappyNumber(number)) {
System.out.println(number + " is a Happy Number.");
} else {
System.out.println(number + " is not a Happy Number.");
}
}
}
π» Testing the Program
To test the program with a different number, replace the value of number in the main method.
19 is a Happy Number.
Compile and run the program to check if the number is a happy number.
π§ How the Program Works
- The program defines a class HappyNumberChecker containing a static method sumOfSquares to calculate the sum of the squares of digits.
- It also defines a static method isHappyNumber that checks whether a given number is a happy number using Floyd's cycle detection algorithm.
- The main method tests the program with a sample number (in this case, 19).
π Between the Given Range
Let's take a look at the java code that checks for Happy Numbers in the specified range.
public class HappyNumberChecker {
// Function to check if a number is happy
static boolean isHappy(int n) {
while (n != 1 && n != 4) {
int sum = 0;
while (n > 0) {
int digit = n % 10;
sum += digit * digit;
n /= 10;
}
n = sum;
}
return n == 1;
}
// Driver program
public static void main(String[] args) {
// Define the range
int start = 1;
int end = 50;
// Print the header
System.out.println("Happy numbers in the range " + start + " to " + end + ":");
// Check and print happy numbers in the range
for (int i = start; i <= end; i++) {
if (isHappy(i)) {
System.out.print(i + " ");
}
}
}
}
π» Testing the Program
Happy numbers in the range 1 to 50: 1 7 10 13 19 23 28 31 32 44 49
Compile and run the program to see the happy numbers within the specified range.
π§ How the Program Works
- The program defines a class HappyNumberChecker containing a static method isHappy to check if a number is happy.
- Inside the main method, it specifies the range from 1 to 50.
- It then checks each number in the range using the isHappy method and prints the happy numbers.
π§ Understanding the Concept of Happy Number
A happy number is a positive integer defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1, or it loops endlessly in a cycle that does not include 1.
For example, let's take the number 19:
- 12+92 = 82
- 82+22 = 68
- 62+82 = 100
- 12+02+02 = 1
In this case, 19 is a happy number because the process ends with 1.
π’ Optimizing the Program
While the provided program is effective, there are various ways to optimize and enhance it further. Consider exploring different algorithms or incorporating additional features based on your specific requirements.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Java Program to Check Happy Number), please comment here. I will help you immediately.