Program to Print all Happy Numbers within a given Range

Python Program to Print all Happy Numbers within a given Range

In this article, we will learn how to print happy numbers within a specific range in Python. You will learn what a happy number is, how to check whether a given number is a happy number, and how to write Python code that outputs all the happy numbers within the user-specified range.

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  = 32 + 22 + 02 = 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 upper limit range =19
Given lower limit range =145

Output:

The happy numbers in the given range 19 and 145 are:
19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100 103 109 129 130 133 139

Example2:

Input:

Given upper limit range =333
Given lower limit range =444

Output:

The happy numbers in the given range 333 and 444 are:
338 356 362 365 367 368 376 379 383 386 391 392 397 404 409 440

Program to Print all Happy numbers within a given range in Python

Below are the ways to print all the happy numbers in the given range.

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Method #1: Using For Loop (Static Input)

Approach:

  • Create a function checkhapppynumb() which accepts the number as an argument and returns true if it is happy number else returns False.
  • Give the lower limit range as static input and store it in a variable.
  • Give the upper limit range as static input and store it in another 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.
  • Loop from lower limit range to upper limit range using For loop.
  • Inside the for loop pass the iterator value to checkhapppynumb() function.
  • Inside the checkhapppynumb() function.
  • Take a variable reslt and initialize its value to the given argument.
  • 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 so return True
  • Else it is not a happy number so return False.
  • If it returns true then print the iterator value with space.
  • 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

# Create a function checkhapppynumb() which accepts the number as an argument
# and returns true if it is happy number else returns False.


def checkhapppynumb(numb):
    # Take a variable rest and initialize its value to the given argument.
    rest = numb
    # Loop till the rest is not equal to 1 or 4 using while loop.
    while(rest != 1 and rest != 4):
        # Inside the loop pass the reslt value to digitSquareSum()
        # and store it in the same variable reslt.
        rest = digitSquareSum(rest)
    # After the end of the while loop,
    # Check whether rest value is 1 or not using the If statement.
    if(rest == 1):
        # If it is true then the given number is a happy number so return True
        return True
    else:
        # Else it is not a happy number so return False.
        return False


# Give the lower limit range as static input and store it in a variable.
lowlimrange = 19
# Give the upper limit range as static input and store it in another variable.
upplimrange = 145
print('The happy numbers in the given range',
      lowlimrange, 'and', upplimrange, 'are:')
# Loop from lower limit range to upper limit range using For loop.
for l in range(lowlimrange, upplimrange+1):
        # IInside the for loop pass the iterator value to checkhapppynumb() function.
    if(checkhapppynumb(l)):
        # If it returns true then print the iterator value.
        print(l, end=' ')

Output:

The happy numbers in the given range 19 and 145 are:
19 23 28 31 32 44 49 68 70 79 82 86 91 94 97 100 103 109 129 130 133 139

Method #2: Using For Loop (User Input)

Approach:

  • Create a function checkhapppynumb() which accepts the number as an argument and returns true if it is happy number else returns False.
  • Give the lower limit range and upper limit range as user input using map(),int(),split() functions.
  • Store them in two separate variables.
  • 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.
  • Loop from lower limit range to upper limit range using For loop.
  • Inside the for loop pass the iterator value to checkhapppynumb() function.
  • Inside the checkhapppynumb() function.
  • Take a variable reslt and initialize its value to the given argument.
  • 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 so return True
  • Else it is not a happy number so return False.
  • If it returns true then print the iterator value.
  • 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

# Create a function checkhapppynumb() which accepts the number as an argument
# and returns true if it is happy number else returns False.


def checkhapppynumb(numb):
    # Take a variable rest and initialize its value to the given argument.
    rest = numb
    # Loop till the rest is not equal to 1 or 4 using while loop.
    while(rest != 1 and rest != 4):
        # Inside the loop pass the reslt value to digitSquareSum()
        # and store it in the same variable reslt.
        rest = digitSquareSum(rest)
    # After the end of the while loop,
    # Check whether rest value is 1 or not using the If statement.
    if(rest == 1):
        # If it is true then the given number is a happy number so return True
        return True
    else:
        # Else it is not a happy number so return False.
        return False


# Give the lower limit range and upper limit range as
# user input using map(),int(),split() functions.
# Store them in two separate variables.
lowlimrange, upplimrange = map(int, input(
    'Enter lower limit range and upper limit range separate bt spaces = ').split())
print('The Harshad numbers in the given range',
      lowlimrange, 'and', upplimrange, 'are:')
print('The happy numbers in the given range',
      lowlimrange, 'and', upplimrange, 'are:')
# Loop from lower limit range to upper limit range using For loop.
for l in range(lowlimrange, upplimrange+1):
        # IInside the for loop pass the iterator value to checkhapppynumb() function.
    if(checkhapppynumb(l)):
        # If it returns true then print the iterator value.
        print(l, end=' ')

Output:

Enter lower limit range and upper limit range separate bt spaces = 333 444
The Harshad numbers in the given range 333 and 444 are:
The happy numbers in the given range 333 and 444 are:
338 356 362 365 367 368 376 379 383 386 391 392 397 404 409 440

Related Programs: