Write a java program to determine whether a given number is happy or not.

A Happy number is that number which results in 1 when the square of its digits is summed up e.g 7, 32, etc and those which are not happy are known as unhappy numbers e.g 2, 4, 16, 37, etc.

So to check for a Happy number we need to add the square of its digits and repeat the process for the summed result if not equal to 1.

The number will be unhappy if the sum never results in 1 and get stuck in the endless loop. That only happens when the sum at any given point is 4.

Let’s do it in Java.

Happy number in Java using while loop


Happy number in Java using Recursion

In this java program, we will check for Happy number using recursion. In each recursive call we will pass the sum of the square of the number digits and if value turns out to be 1 then return true and if the value is 4 return false.


If you like this java solution for Happy number then bookmark the site or comment below your suggestion or doubts.

Leave a Reply

Close Menu