Python Program to Check If A Number Is A Happy Number

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Given a number, the task is to check whether the given number is a happy number or not.

Happy Number :

If the repeating sum of the digits squared equals one, the number is said to be a Happy Number. If we continue this method and get outcome 1, we have a happy number. If the outcome is 4, it enters an infinite loop and is not a happy number. Let’s look at an example to help you grasp it better.

Given Number = 320
Square of the digits  = 3+ 22 + 0= 13
Square of the digits  = 12 + 32 = 10
Square of the digits  = 12 + 02 = 1

Other Examples of happy numbers  =7, 28, 100 etc.

Examples:

Example1:

Input:

Given number =100

Output:

The given number [ 100 ] is a happy number

Example2:

Input:

Given number = 28

Output:

The given number [ 28 ] is a happy number

Program to Check If A Number Is A Happy Number in Python

Below are the ways to check whether the given number is a happy number or not.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Create a function digitSquareSum() that accepts the given number as an argument and returns the sum of squares of digits.
  • Inside the digitSquareSum() function.
  • Convert the given argument to string using the str() function.
  • Convert the given argument into list of digits using list(),map(),int() functions.
  • Store it in a variable.
  • Take a variable sumsquaredigits and initialize its value to 0.
  • Loop in this digits list using For loop.
  • Increment the value of sumsquaredigits by the square of the digit(iterator value).
  • Return the sumsquaredigits value.
  • Take a variable reslt and initialize its value to the given number.
  • Loop till the reslt is not equal to 1 or 4 using while loop.
  • Inside the loop pass the reslt value to digitSquareSum() and store it in the same variable reslt.
  • After the end of the while loop, Check whether reslt value is 1 or not using the If statement.
  • If it is true then the given number is a happy number.
  • Else it is not a happy number.
  • The Exit of the Program.

Below is the implementation:

# Create a function digitSquareSum() that accepts the given number
# as an argument and returns the sum of squares of digits.


def digitSquareSum(resltnumber):
    # Inside the digitSquareSum() function.
    # Convert the given argument to string using the str() function.
    strnumbe = str(resltnumber)
    # Convert the given argument into list of digits using list(),map(),int() functions.
    # Store it in a variable.
    numbrlistdigits = list(map(int, strnumbe))
    # Take a variable sumsquaredigits and initialize its value to 0.
    sumsquaredigits = 0
    # Loop in this digits list using For loop.
    for digitvalu in numbrlistdigits:
        # Increment the value of sumsquaredigits by the square
        # of the digit(iterator value).
        sumsquaredigits = sumsquaredigits+(digitvalu**2)
    # Return the sumsquaredigits value
    return sumsquaredigits


# Give the number as static input and store it in a variable.
numbr = 100
# Take a variable reslt and initialize its value to the given number.
reslt = numbr
# Loop till the reslt is not equal to 1 or 4 using while loop.
while(reslt != 1 and reslt != 4):
    # Inside the loop pass the reslt value to digitSquareSum()
    # and store it in the same variable reslt.
    reslt = digitSquareSum(reslt)
# After the end of the while loop,
# Check whether reslt value is 1 or not using the If statement.
if(reslt == 1):
    # If it is true then the given number is a happy number.
    print('The given number [', numbr, '] is a happy number')
else:
    # Else it is not a happy number.
    print('The given number [', numbr, '] is not a happy number')

Output:

The given number [ 100 ] is a happy number

Method #2: Using For Loop (User Input)

Approach:

  • Give the number as user input using int(input()) and store it in a variable.
  • Create a function digitSquareSum() that accepts the given number as an argument and returns the sum of squares of digits.
  • Inside the digitSquareSum() function.
  • Convert the given argument to string using the str() function.
  • Convert the given argument into list of digits using list(),map(),int() functions.
  • Store it in a variable.
  • Take a variable sumsquaredigits and initialize its value to 0.
  • Loop in this digits list using For loop.
  • Increment the value of sumsquaredigits by the square of the digit(iterator value).
  • Return the sumsquaredigits value.
  • Take a variable reslt and initialize its value to the given number.
  • Loop till the reslt is not equal to 1 or 4 using while loop.
  • Inside the loop pass the reslt value to digitSquareSum() and store it in the same variable reslt.
  • After the end of the while loop, Check whether reslt value is 1 or not using the If statement.
  • If it is true then the given number is a happy number.
  • Else it is not a happy number.
  • The Exit of the Program.

Below is the implementation:

# Create a function digitSquareSum() that accepts the given number
# as an argument and returns the sum of squares of digits.


def digitSquareSum(resltnumber):
    # Inside the digitSquareSum() function.
    # Convert the given argument to string using the str() function.
    strnumbe = str(resltnumber)
    # Convert the given argument into list of digits using list(),map(),int() functions.
    # Store it in a variable.
    numbrlistdigits = list(map(int, strnumbe))
    # Take a variable sumsquaredigits and initialize its value to 0.
    sumsquaredigits = 0
    # Loop in this digits list using For loop.
    for digitvalu in numbrlistdigits:
        # Increment the value of sumsquaredigits by the square
        # of the digit(iterator value).
        sumsquaredigits = sumsquaredigits+(digitvalu**2)
    # Return the sumsquaredigits value
    return sumsquaredigits


# Give the number as user input using int(input()) and store it in a variable.
numbr = int(input(
    'Enter some random number to check whether the number is happy number or not = '))
# Take a variable reslt and initialize its value to the given number.
reslt = numbr
# Loop till the reslt is not equal to 1 or 4 using while loop.
while(reslt != 1 and reslt != 4):
    # Inside the loop pass the reslt value to digitSquareSum()
    # and store it in the same variable reslt.
    reslt = digitSquareSum(reslt)
# After the end of the while loop,
# Check whether reslt value is 1 or not using the If statement.
if(reslt == 1):
    # If it is true then the given number is a happy number.
    print('The given number [', numbr, '] is a happy number')
else:
    # Else it is not a happy number.
    print('The given number [', numbr, '] is not a happy number')

Output:

The given number [ 100 ] is a happy number

Related Programs: