Program to check if the given number is Happy Number in Python

Program to check if the given number is Happy Number in Python. This program contains functions, if else conditional statements and while loop. This program is for beginners, class 11,12 and B Tech students. Download free source code from this site.

What is Happy numbers ?

In number theory, a Happy number is a number which eventually reaches 1 when replaced by the sum of the square of each digit. For instance, 13 is a happy number because {\displaystyle 1^{2}+3^{2}=10}, and {\displaystyle 1^{2}+0^{2}=1}. On the other hand, 4 is not a happy number because the sequence starting with {\displaystyle 4^{2}=16} and {\displaystyle 1^{2}+6^{2}=37} eventually reaches {\displaystyle 2^{2}+0^{2}=4}, the number that started the sequence, and so the process continues in an infinite cycle without ever reaching 1. A number which is not happy is called sad or unhappy.

Source code :

   
def isHappyNumber(num):    
    rem = sum = 0    
        
        
    while(num > 0):    
        rem = num%10    
        sum = sum + (rem*rem)    
        num = num//10;    
    return sum;    
        
num = int(input("Enter the n.o to checked happy or sad"))   
result = num  

while(result != 1 and result != 4):    
    result = isHappyNumber(result)    
     
    
if(result == 1):    
    print(str(num) + " is a happy number")    
    
elif(result == 4):    
    print(str(num) + " is not a happy number")
 

Step wise source code Explanation :

Step 1: isHappyNumber() will determine whether a number is happy or not
def isHappyNumber(num):
rem = sum = 0
Step 2: Calculates the sum of squares of digits
while(num > 0):
rem = num%10
sum = sum + (rem*rem)
num = num//10;
return sum;
num = int(input(“Enter the n.o to checked happy or sad”))
result = num
while(result != 1 and result != 4):
result = isHappyNumber(result)
Step 3: Happy number always ends with 1
if(result == 1):
print(str(num) + ” is a happy number”)
Step 4:Unhappy number ends in a cycle of repeating numbers which contain 4
elif(result == 4):
print(str(num) + ” is not a happy number”)

Output :

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
 RESTART: C:/csstudy/download source code files/Program to check if the given number is Happy Number in Python.py 
Enter the n.o to checked happy or sad37
37 is not a happy number
>>> 
 RESTART: C:/csstudy/download source code files/Program to check if the given number is Happy Number in Python.py 
Enter the n.o to checked happy or sad32
32 is a happy number
>>> 

Output Explanation:

Step 1 :Input is captured
Enter the n.o to checked happy or sad32
Step 2 :After calculation in the backend it is printed on the screen
32 is a happy number

Conclusion :

This program runs fine.

Thank you for reading, learning and downloading Source code of Program to check if the given number is Happy Number in Python. This program contains functions, if else conditional statements and while loop. This program is for beginners, class 11,12 and B Tech students. For more programs click below-
Python Programs Archives – CS Study
Computer Science with Python Practical Book Solutions Class 11 Term -2 – CS Study
Computer Science Practical file for class 12 Term-2 – CS Study

Leave a Comment

Your email address will not be published.