Learn about the Program to Print Perfect Numbers from 1 to 100 in Python in the below code example. Also, refer to the comments in the code snippet to get a detailed view of what’s actually happening.
Perfect Number :
A perfect number is a number for which sum of all positive divisors excluding the number itself is equal to that number.
Program to Print Perfect Numbers from 1 to 100 in Python
We start by reading the user’s min value and max value. The function is perfect() determines if a given number is perfect or not. We loop from min value to max value and feed each number to the is perfect() method. If this method returns True, we print it.
# Program to print perfect numbers from 1 to 100 def perfect(n): if n < 1: return False perfect_sum = 0 for i in range(1,n): if n%i==0: perfect_sum += i return perfect_sum == n min_value = 1 max_value = 100 for i in range(min_value, max_value+1): if perfect(i): print(i)
Hope the above code works for you and Refer to the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code : Palindrome Number using Slicing in Python