# Happy Number program in Java

In this article, we are going to learn to find `Happy Number` using Java. Let’s first understand, what is Happy Number?

## 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)

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

1. Take one variable `sum` for storing sum of square.
2. Intialize a HashSet `numbers`, we will use this HashSet to track repeating cycle.
3. Iteate through while loop until we get same number again and do following:
1. Calculate the square of each digit present in the number and add it to the variable `sum` and divide the `number` by 10.
2. Assign `sum` to `number` after each iteration
4. If result `sum` is equal to 1, then the number is a `Happy Number`.
5. 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,

### Example

Here is complete example to find Happy number in java.

Output

It is a happy number

## Using slow and fast pointers

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

### Algorithm

1. initialize two variables `slow` and `fast` with given number.
2. Iterate through do while loop until we get a cycle (`slow!=fast`)
1. Move `slow` pointer once by calling `getSumOfSquareOfDigit()` once.
2. Move `fast` pointer twice by calling `getSumOfSquareOfDigit()` twice.
3. 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.

Output

1111111 is a happy number

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

import_contacts

### You may also like: ## Related Posts

• 05 April

### Count occurrences of Character in String in Java

Table of Contents1. Using String Library Methods2. Using Recursion3. Using Hashing ConceptUsing ArraysUsing Collections (Map) 4. Using Java 8 Features In this article, we will look at a problem: Given an Input String and a Character, we have to Count Occurrences Of character in String in java. For Example, If the Given String is : […]

• 24 December

### How to capitalize first letter in java

Learn about how to capitalize first letter in java.

• 09 October

### Java program to find first and last digit of a number

Table of ContentsAlgorithmUsing while loopUsing log() and pow() methodsUsing while loop and pow() method In this article, we are going to find first and last digit of a number in Java. To find first and last digit of any number, we can have several ways like using modulo operator or pow() and log() methods of […]

• 30 September

### Find Perfect Number in Java

Table of ContentsIterative approachRecursive approach In this article, we are going to find whether a number is perfect or not using Java. A number is called a perfect number if the sum of its divisors is equal to the number. The sum of divisors excludes the number. There may be several approaches to find the […]

• 28 September

### How to find Magic Number in Java

Table of ContentsWhat is a Magic Number?Algorithm for Magic NumberExample to find Magic NumberAnother Example To find Magic Number In this article, we are going to learn to find Magic Number using Java. Let’s first understand, what is Magic Number? What is a Magic Number? A number which leaves 1 as a result after a […]

• 28 September

### Number guessing game in java

Table of ContentsNumber guessing game RulesAlgorithm for Number guessing game In this article, we will implement Number guessing game in java. The number guessing game is based on a concept where player guesses a number between a range. If player guesses the exact number then player wins else player looses the game. Since this game […]

## Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.