Happy Number Program in Java with Examples

In this blog post we will learn about happy number program in java. Happy numbers definition depends on the decimal representation (base 10) of the number. The definition of happy numbers can be extended to other bases for e.g base 5. In this article I will be sharing what is Happy number, what is unhappy number(sad number) with examples and the program to check happy number in java.

What is Happy number

Any positive number greater than zero(positive integer) is called happy number, if we repeatedly replace the number, by sum of square of its digits till the final result becomes 1
Examples :


19 is a happy number 

12+92 = 82 
82+22 = 68
62+82 = 100
12+02+02 = 1

31 is a happy number 

32+12 = 10 
12+02 = 1

If the process loops endlessly in a cycle that does not include 1 then the number is called unhappy number or sad number.  In other words, those numbers which does not end in 1 in the above process are unhappy numbers or sad numbers.

Some of the examples of unhappy number are : 11 , 12 , 30

Happy numbers upto 100 are : 1, 7, 10, 13, 19, 23, 28, 31, 32, 44, 49, 68, 70, 79, 82, 86, 91, 94, 97, 100 

Happy Number Program in Java :


import java.util.Set;
import java.util.HashSet;
import java.util.Scanner;

public class HappyNumber {
    
    public static void main(String args[]) {
        System.out.println("Enter any number : ");
        // Input the number 
        int inputNumber = new Scanner(System.in).nextInt();
        Boolean result = isHappyNumber(inputNumber);
        if (result == true)
            System.out.print(inputNumber + " is a Happy number");
        else    
            System.out.print(inputNumber + " is an Unhappy number");
    }

    public static boolean isHappyNumber(int number) {
        Set<Integer> seenNumbers = new HashSet<Integer>();
        while(seenNumbers.add(number)){
            int digitsSum = 0;
            while(number > 0) {
                digitsSum += Math.pow(number % 10 , 2);
                number /= 10;
            }
            number = digitsSum;
            if(number == 1)
                return true;
        }
        return false;
    }  
}


Output :

Enter any number : 86
86 is a Happy number 

Code Explanation :

To check whether an input number is happy or not :

1. Calculate the square of each digit present in the inputNumber. Add it to a variable digitsSum.
  • If the resulting value of digitsSum is 1 then the input number is happy number.
  • Else the number is unhappy number.    

Points To Remember :

1. According to Wikipedia, the happiness of a number remains unaffected by inserting or removing any number of zeros anywhere in the number and by rearranging the digits. 

2. The members of the sequence are happy, if the given number is happy. For example 7 is a happy number as shown below, so sequences 49, 97, 130, 10 are also happy numbers.

7 is a happy number as shown below

02+72 = 49 
42+92 = 97
92+72 = 130
12+32+02 = 10
12+02 = 1

Similarly, if a number is unhappy, then all of its sequence members are also unhappy numbers.

That's all for this post. Please mention in comments if you have any questions related to happy number program in java with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry