In this article, we are going to learn to find `Happy Number`

using Java. Let’s first understand, what is Happy Number?

Table of Contents

## What is a Happy Number?

A number which leaves 1 as a result after a sequence of steps and in each step number is replaced by the sum of squares of its digit. For example, if we check whether `23`

is a Happy Number, then sequence of steps are

`Step 1:`

2×2+3×3 = 4+9 = 13 **// Sum of square of each digit**

`Step 2:`

1×1+3×3 = 1+9 = 10`Step 3:`

1×1+0x0 = 1 (A Happy Number)**💻 Awesome Tech Resources:**

- Looking for ⚒️ tech jobs? Go to our job portal.
- Looking for tech events? Go to tech events 🗓️ Calendar.️

We will see two approaches to find happy number in java.

## Using Hashset

In this approach, we will use Hashset to tract the cycle and if `sum=1`

at the end of the cycle, then it is happy number.

### Algorithm

- Take one variable
`sum`

for storing sum of square. - Intialize a HashSet
`numbers`

, we will use this HashSet to track repeating cycle. - Iteate through while loop until we get same number again and do following:
- Calculate the square of each digit present in the number and add it to the variable
`sum`

and divide the`number`

by 10. - Assign
`sum`

to`number`

after each iteration

- Calculate the square of each digit present in the number and add it to the variable
- If result
`sum`

is equal to 1, then the number is a`Happy Number`

. - Else the number is not happy Number.

Note:A number can not be a Happy Number if it makes a loop in its sequence. For example,

12345678910111213141516171819 4x4 = 161x1+6x6 = 373x3+7x7 = 585x5+8x8 = 898x8+9x9 = 1451x1+4x4+5x5 = 424x4+2x2 = 202x2+0x0 = 4 // Number itself4 = 16 // Repeating process (loop), see step 1

### Example

Here is complete example to find Happy number in java.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.HashSet; import java.util.Set; public class Main { public static void main(String args[]) { int number = 1111111; int sum = 0; Set<Integer> numbers = new HashSet<Integer>(); while(numbers.add(number)){ sum = 0; while(number>0) { sum += (number % 10)*(number % 10); number /=10; } number = sum; } // if sum = 1, it is happy number if(sum == 1) { System.out.println("It is a happy number"); }else { System.out.println("It is not a happy number"); } } } |

Output

## Using slow and fast pointers

In this approach, we will use slow and fast pointer to track the cycle.

### Algorithm

- initialize two variables
`slow`

and`fast`

with given number. - Iterate through do while loop until we get a cycle (
`slow!=fast`

)- Move
`slow`

pointer once by calling`getSumOfSquareOfDigit()`

once. - Move
`fast`

pointer twice by calling`getSumOfSquareOfDigit()`

twice.

- Move
- If
`slow`

is equal to 1 at the end of cycle, it means it is`happy number`

.

### Example

Here is complete example to find Happy number in java.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package org.arpit.java2blog; public class HappyNumberSlowFast { static int getSumOfSquareOfDigit(int num) { int ss = 0; while (num != 0) { ss += (num % 10) * (num % 10); num /= 10; } return ss; } // method return true if n is Happy number static boolean isHappynumber(int num) { int slow, fast; // initialize slow and fast by num slow = fast = num; do { // move slow number by one step slow = getSumOfSquareOfDigit(slow); // move fast number by two steps fast = getSumOfSquareOfDigit(getSumOfSquareOfDigit(fast)); } while (slow != fast); // If slow and fast point meet and slow is equals to 1, // then it is happy number return (slow == 1); } // Main method public static void main(String[] args) { int n = 1111111; if (isHappynumber(n)) System.out.println(n + " is a Happy number"); else System.out.println(n + " is not a Happy number"); } } |

Output

That’s all about How to find Happy Number in Java