Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Happy Number problem solution

YASH PAL, 31 July 2024

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

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

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

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes