Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Happy Number problem solution

YASH PAL, 31 July 202419 January 2026

In this Leetcode Happy Number problem solution Write an algorithm to determine if a number n is happy.

  1. A happy number is a number defined by the following process:
  2. Starting with any positive integer, replace the number with the sum of the squares of its digits.
  3. Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
  4. Those numbers for which this process ends in 1 are happy.
  5. Return true if n is a happy number, and false if not.
Leetcode Happy Number problem solution

Leetcode Happy Number problem solution in Python.

class Solution(object):
    def isHappy(self, n, prev=[]):
        nums = sum([int(x)**2 for x in list(str(n))])
        if nums in prev:
            return False
        return self.isHappy(nums, prev+[nums]) if nums != 1 else True

Happy Number problem solution in Java.

class Solution {
    public boolean isHappy(int n) {
        HashSet<Integer> set = new HashSet<>();
        while(n != 1){
            if(set.contains(n)){
                return false;
            }
            set.add(n);
            n = sum(n);
        }
        return true;
    }
    
    public int sum(int n){
        int sum = 0;
        while(n > 0){
            sum += (n%10) * (n%10);
            n = n/10;
        }
        return sum;
    }
}

Problem solution in C++.

class Solution {
public:
    map<int, int> terminate;
    bool isHappy(int n) {
        int temp = 0;
        if(n==1){
            return 1;
        }
        while(n!=0){
            temp += pow(n%10, 2);
            n = n/10;
        }
        if(terminate[temp] == 0){
            terminate[temp] = 1;
        } else{
            return 0;
        }
        return isHappy(temp);
    }
};

Problem solution in C.

int key[1000],i = 0;
int count = 0,temp;
while(i<1000)       //Initialize
	key[i++]=1;

for(;n;n/=10)            
	{
		temp=n%10;
		count+=temp*temp;
	}
if(count==1) return true;

n=count;
while(key[n])
{
	
	int x=n;
	count=0;
	for(;n;n/=10)
		{
			temp=n%10;
			count+=temp*temp;
		}
	if(count==1) return true;
	else
	{
		key[x]=0;
		n=count;
	}
}
return false;

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes