Python Program to all Perfect Numbers in Given Range using While Loop
This example shows, how to print all perfect numbers in given range using while loop in python.
Steps
- The while loop starts and will continue to run as long as num is less than or equal to limit. This loop checks each number from 1 up to the specified Limit.
- Inside the inner loop, the code checks if divisor is a proper divisor of num using the modulo operator (%). If num modulo divisor is 0, it means divisor is a proper divisor.
- If divisor is a proper divisor, it is added to sum.
Example:
Example.py
Limit = int(input("Enter the Limit of the range: ")) num = 1 while num <= Limit: sum = 0 divisor = 1 while divisor < num: if num % divisor == 0: sum += divisor divisor += 1 if sum == num: print(num, "is a perfect number") num += 1
Output
Enter the Limit of the range: 500 6 is a perfect number 28 is a perfect number 496 is a perfect number