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


When it is required to find all numbers in a range where there are perfect square, and sum of digits in the number is less than 10, list comprehension is used.

Below is the demonstration of the same −

Example

 Live Demo

lower_limit = int(input(“Enter the lower range: “))
upper_limit = int(input(“Enter the upper range: “))
my_list = []
my_list = [x for x in range(lower_limit,upper_limit+1) if (int(x**0.5))**2==x and
sum(list(map(int,str(x))))<10]
print(“The result is : “)
print(my_list)

Output

Enter the lower range: 5
Enter the upper range: 12
The result is :
[9]

Explanation

  • The lower range and upper range are taken by the user.

  • An empty list is defined.

  • The list comprehension is used, to iterate over the lower and upper limits.

  • The square root of the elements are found.

  • The elements are summed up.

  • It is converted to a list.

  • This is assigned to a variable.

  • The output is displayed on the console.

Updated on: 19-Apr-2021

553 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements