Doesn't the name “Happy number” itself make you curious about Happy numbers? What are these numbers? How do we know if a given number is a happy number? We will find answers to all these questions today.

In this article, we will learn about Happy numbers in Java and write a program to check whether a number entered is a Happy number.

So, let’s get started!

What is a Happy Number?

A happy number is a number that yields 1 when substituted by the sum of its digits, squared repeatedly. If this method produces an infinite cycle of numbers containing 4, the number is known as an unhappy number. We will look at the implementation of finding out whether a number is a happy number in java.

For example, 49 is a happy number since the procedure results in 1, as seen below.

More examples of happy numbers are 7, 28, 32, 100, 320, etc.

Get the tech career you deserve, faster!

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

What is a Happy Number in Java?

A digit is called happy if the sum of the square of the digit gives 1 after the sequence of steps, and in each step, the number is replaced by the previous sum of the square. Let's understand it with an example: 13-> 1^2 + 3^2 = 10 10-> 1^2 + 0^2 = 1 Above, we see that the number eventually gives us 1 as the output, and hence the number 13 is a happy number.

The Logic Behind Happy Numbers in Java

Problem Statement

Write a Program to determine whether a given number N is a happy number in Java.

Algorithm

Below is the algorithm to determine whether a number is a happy number or not. Follow the steps below:

Take the number to be tested from the keyboard and store it in some variable, say num.

Pass this variable to a function called isNumHappy(), which returns the final sum value.

Inside the function,

Divide(%) num by ten and store the remainder in another variable, say rem.

Declare a variable sum(initialize with 0) and add the square of rem to it.

Now, divide(/) the num by 10.

Repeat steps 2–4 until we get the sum of the squares of all the digits in the given number num.

The final addition value is then stored in the variable sum.

Return sum.

Create a variable named finalVal and initialize it with num.

If finalVal is not equal to 1 and 4, then simply call the isNumHappy() function by passing the finalVal as a parameter and storing the result in finalVal itself.

If finalVal’s value becomes 1, print “The number entered is a Happy number”; else, print “The number entered is not a Happy number.”

Java Implementation

Below is the implementation to find happy number in java.

import java.util.*;
public class HappyNum {
public static int isNumHappy(int num) {
int rem = 0, sum = 0;
// calculate the sum of squares of each digit
while (num > 0) {
rem = num % 10;
sum = sum + (rem * rem);
num = num / 10;
}
return sum;
}
public static void main(String[] args) {
// Take num
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number greater than 0:");
int num = sc.nextInt();
int finalVal = num;
while (finalVal != 1 && finalVal != 4) {
finalVal = isNumHappy(finalVal);
}
if (finalVal == 1) {
System.out.println("The number entered is a Happy Number");
} else {
System.out.println(" The number entered is not a Happy Number");
}
}
}

Output

Enter a number greater than 0:15
The number entered is not a Happy Number
Enter a number greater than 0: 10
10
The number entered is a Happy Number

Time complexity

O(log N). Here, N represents the number of digits in the given number num.

Space Complexity

O(1), as no extra space is needed.

Implementation in Python

def check_happy_number(i):
check = set()
while i != 1:
i = sum(int(j)**2 for j in str(i))
if i in check:
return False
check.add(i)
return True
print(check_happy_number(7))
print(check_happy_number(932))
print(check_happy_number(6))

Output

True
True
False

Implementation in C

#include <stdio.h>
int check_happy_number(int num){
int rem = 0, sum = 0;
while(num > 0){
rem = num%10;
sum = sum + (rem*rem);
num = num/10;
}
return sum;
}
int main()
{
int num = 13;
int result = num;
while(result != 1 && result != 4){
result = check_happy_number(result);
}
if(result == 1)
printf("%d is a happy number", num);
else if(result == 4)
printf("%d not is a happy number", num);
return 0;
}

Output

13 is a happy number

Implementation in C#

using System;
public class Happy_Number
{
public static int check_happy_number(int num){
int rem = 0, sum = 0;
while(num > 0){
rem = num%10;
sum = sum + (rem*rem);
num = num/10;
}
return sum;
}
public static void Main()
{
int num = 13;
int result = num;
while(result != 1 && result != 4){
result = check_happy_number(result);
}
if(result == 1)
Console.WriteLine(num + " : is a happy number");
else if(result == 4)
Console.WriteLine(num + " : is not a happy number");
}
}

To check if a number is happy in Java, first, calculate the sum of the squares of its digits. If the result is 1, the number is happy. If not, repeat the process until either 1 is reached or the same sum is repeated.

How do you work out a happy number?

The logic of a happy number is to repeatedly sum the squares of its digits until the result is either 1 or a previously obtained sum. If the result is 1, the number is happy. If not, it is an unhappy number.

What is the range of happy numbers in Java?

The range of happy numbers is not defined as such mostly it depends upon the input values. A happy number is a number that yields 1 when substituted by the sum of its digits, squared repeatedly.

Are there an infinite number of happy numbers?

Right now it is not proven that there are infinite numbers of happy numbers and there is a hypothesis that all numbers eventually end up in a cycle that includes either 4 or 1, with 1 being the happy number in it.

Conclusion

In this article, we learned about Happy numbers in Java. We also implemented a Java code to check whether a given number is Happy or not.

We hope this article has clarified your understanding of Happy number in Java. You can refer to our blogs to understand more about Java concepts.