Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10

Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10

Lists in Python:

A list is exactly what it sounds like: a container for various Python objects such as integers, words, values, and so on. In other programming languages, it is equal to an array. It is denoted with square brackets (and this is one of the attributes that differentiates it from tuples, which are separated by parentheses). It is also mutable, which means it can be changed or altered, as opposed to tuples, which are immutable.

Given lower limit and upper limit , the task is to print all numbers in the given range which are perfect squares and sum of all Digits in the Number is Less than 10.

Examples:

Example1:

Input:

Enter some random lower limit =5
Enter some random lower limit =525

Output:

Printing the perfect squares numbers list which are less than 10: [9, 16, 25, 36, 81, 100, 121, 144, 225, 324, 400, 441]

Example2:

Input:

Enter some random lower limit =69
Enter some random lower limit =2379

Output:

Printing the perfect squares numbers list which are less than 10: [81, 100, 121, 144, 225, 324, 400, 441, 900, 1024, 1521, 1600, 2025, 2304]

Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits in the Number is Less than 10 in Python

There are several ways to find all all numbers in the given range which are perfect squares and sum of all Digits in the Number is Less than 10 and store them in the list some of them are:

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Method #1:Using List Comprehension (Static Input)

Approach:

  • Give the lower limit range and upper limit range as static input.
  • When the element is a perfect square within the range and the total of the digits of the number is less than 10, a list must be generated using list comprehension.
  • After that, the list must be printed.
  • Exit of program

Below is the implementation:

# given lower limit range as static input
lowerlimit = 5
# given upper limit range as static input
upperlimit = 525
# When the element is a perfect square within the range and the total of the digits of the number
# is less than 10, a list must be generated using list comprehension.
prftSquaresList = [k for k in range(
    lowerlimit, upperlimit+1) if (int(k**0.5))**2 == k and sum(list(map(int, str(k)))) < 10]
# printing the list
print("Printing the perfect squares numbers list which are less than 10: ", prftSquaresList)

Output:

Printing the perfect squares numbers list which are less than 10:  [9, 16, 25, 36, 81, 100, 121, 144, 225, 324, 400, 441]

Method #2:Using List Comprehension (User Input)

Approach:

  • Scan the lower limit range and upper limit range as user input using int(input()) which converts string to integer.
  • When the element is a perfect square within the range and the total of the digits of the number is less than 10, a list must be generated using list comprehension.
  • After that, the list must be printed.
  • Exit of program.

Below is the implementation:

# Scan lower limit range as user input
lowerlimit = int(input("Enter some random lower limit ="))
# Scan upper limit range as user  input
upperlimit = int(input("Enter some random lower limit ="))
# When the element is a perfect square within the range and the total of the digits of the number
# is less than 10, a list must be generated using list comprehension.
prftSquaresList = [k for k in range(
    lowerlimit, upperlimit+1) if (int(k**0.5))**2 == k and sum(list(map(int, str(k)))) < 10]
# printing the list
print("Printing the perfect squares numbers list which are less than 10: ", prftSquaresList)

Output:

Enter some random lower limit =69
Enter some random lower limit =2379
Printing the perfect squares numbers list which are less than 10: [81, 100, 121, 144, 225, 324, 400, 441, 900, 1024,
 1521, 1600, 2025, 2304]

Explanation:

  • The upper and lower ranges for the numbers must be entered by the user.
  • To make a list of numbers that are perfect squares and have a sum of digits fewer than ten, list comprehension must be utilized.
  • The second stage of the list comprehension process involves mapping individual digits of a number into a list and calculating the sum of the list’s elements.
  • The list that has been generated is printed.

Method #3:Using for loop (Static Input)

Approach:

  • Give the lower limit range and upper limit range as static input.
  • Take a empty list say prftSquaresList 
  • Use a for loop that goes from the lower limit range to the upper range using range() function.
  • When the element is a perfect square within the range and the total of the digits of the number is less than 10 then append that element to the prftSquaresList  using append() function.
  • After that, the list must be printed.
  • Exit of program

Below is the implementation:

# given lower limit
# range as static input
lowerlimit = 5
# given upper limit range as static input
upperlimit = 525
# Take a empty list say prftSquaresList 
prftSquaresList = []
# Use a for loop that goes from the lower limit range to the upper range using
# range() function.
for numb in range(lowerlimit, upperlimit+1):
    # When the element is a perfect square within the range and the total of the digits of the number is less than 10
    # then append that element to the prftSquaresList  using append() function.
    if((int(numb**0.5))**2 == numb and sum(list(map(int, str(numb)))) < 10):
        prftSquaresList.append(numb)


# printing the list
print("Printing the perfect squares numbers list which are less than 10: ", prftSquaresList)

Output:

Printing the perfect squares numbers list which are less than 10:  [9, 16, 25, 36, 81, 100, 121, 144, 225, 324, 400,
 441]

Method #4:Using for loop (User Input)

Approach:

  • Scan the lower limit range and upper limit range as user input using int(input()) which converts string to integer.
  • Take a empty list say prftSquaresList 
  • Use a for loop that goes from the lower limit range to the upper range using range() function.
  • When the element is a perfect square within the range and the total of the digits of the number is less than 10 then append that element to the prftSquaresList  using append() function.
  • After that, the list must be printed.
  • Exit of program

Below is the implementation:

# Scan lower limit range as user input
lowerlimit = int(input("Enter some random lower limit ="))
# Scan upper limit range as user  input
upperlimit = int(input("Enter some random lower limit ="))
# Take a empty list say prftSquaresList 
prftSquaresList = []
# Use a for loop that goes from the lower limit range to the upper range using
# range() function.
for numb in range(lowerlimit, upperlimit+1):
    # When the element is a perfect square within the range and the total of the digits of the number is less than 10
    # then append that element to the prftSquaresList  using append() function.
    if((int(numb**0.5))**2 == numb and sum(list(map(int, str(numb)))) < 10):
        prftSquaresList.append(numb)


# printing the list
print("Printing the perfect squares numbers list which are less than 10: ", prftSquaresList)

Output:

Enter some random lower limit =127
Enter some random lower limit =2379
Printing the perfect squares numbers list which are less than 10: [144, 225, 324, 400, 441, 900, 1024, 1521, 1600, 2025, 2304]