# Happy Number LeetCode Solution

Here, We see Happy Number problem Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc., with different approaches.

## Problem Statement ->

Write an algorithm to determine if a number n is happy.

happy number is a number defined by the following process:

• Starting with any positive integer, replace the number by the sum of the squares of its digits.
• Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
• Those numbers for which this process ends in 1 are happy.

Return true if n is a happy number, and false if not.

```Example 1:
Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1

Example 2:
Input: n = 2
Output: false```

## Happy Number Leetcode Solution C++ ->

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```class Solution {
public:
int solve(int n) {
int sum = 0;
while(n > 0) {
int r = n%10;
sum += r*r;
n /= 10;
}
return sum;
}

bool isHappy(int n) {
int slow = n, fast = n;
do {
slow = solve(slow);
fast = solve(solve(fast));
if(fast == 1) return 1;
} while(slow != fast);
return 0;
}
};
```Code language: C++ (cpp)```

## Happy Number Leetcode Solution Java ->

``````class Solution {
public boolean isHappy(int n) {
if(n<=0) return false;
while(true){
int sum=0;
while(n!=0){
sum+=(n%10)*(n%10);
n=n/10;
}
if(sum/10==0){
if(sum==1||sum==7) return true;
else return false;
}
n=sum;
}
}
}
```Code language: Java (java)```

## Happy Number Leetcode Solution JavaScript ->

``````var isHappy = function(n) {
var seen = {};
while (n !== 1 && !seen[n]) {
seen[n] = true;
n = sumOfSquares(n);
}
return n === 1 ? true : false;
};

function sumOfSquares(numString) {
return numString.toString().split('').reduce(function(sum, num) {
return sum + Math.pow(num, 2);
}, 0);
};
```Code language: JavaScript (javascript)```

## Happy Number Leetcode Solution Python ->

``````class Solution(object):
def isHappy(self, n):
total = 0
for digit_str in str(n):
digit = int(digit_str)
total += digit * digit

if total == 1 or total == 7:
return True
else:
if total < 10 or total == 0:
return False
else:
return self.isHappy(total)
```Code language: Python (python)```

#### Jump Game II LeetCode Solution

You are given a 0-indexed array of integers nums of length n. You are initially…

#### Permutations LeetCode Solution

Given an array nums of distinct integers, return all the possible permutations. You can return…

#### Two Sum LeetCode Solution

Given an array of integers nums and an integer target, return indices of the two…

#### Search in Rotated Sorted Array LeetCode Solution

Given the array nums after the possible rotation and an integer target, return the index…

#### Valid Parentheses LeetCode Solution

Given a string s containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid.

#### Word Search II LeetCode Solution

Given an m x n board of characters and a list of strings words, return all words on the board.