In this Python program, we will learn how to check if the given number is a perfect number or not
A perfect number is a positive integer that is equal to the sum of its positive divisors, excluding the number itself.
For Example, 6 has divisors 1, 2 and 3 (excluding itself), and 1 + 2 + 3 = 6, so 6 is a perfect number.
The sum of divisors of a number, excluding the number itself, is called its aliquot sum, so a perfect number is one that is equal to its aliquot sum. In other words, a perfect number is a number that is half the sum of all of its positive divisors including itself
I.e. σ1(n) = 2n
For Example, 28 is perfect as 1 + 2 + 4 + 7 + 14 + 28 = 56 = 2 × 28
Here is the source code of the program to check if the given number is a perfect number or not.
Python Program to find the Number is Perfect Number or Not Using For Loop
# Python Program to find the Number is Perfect Number or Not Using For Loop
# Take the Input from the User
Number = int(input("Enter any Number: "))
Sum = 0
for i in range(1, Number):
if(Number % i == 0):
Sum = Sum + i
if (Sum == Number):
print(" %d is a Perfect Number" %Number)
else:
print(" %d is not a Perfect Number" %Number)
Enter any Number: 28
28 is a Perfect Number
Enter any Number: 45
45 is not the Perfect Number
Python Program to find the Number is Perfect Number or Not Using While Loop
# Python Program to find the Number is Perfect Number or Not Using While Loop
# Take the input from the User
Number = int(input("Enter any Number: "))
i = 1
Sum = 0
while(i < Number):
if(Number % i == 0):
Sum = Sum + i
i = i + 1
if (Sum == Number):
print("%d is a Perfect Number" %Number)
else:
print("%d is not the Perfect Number" %Number)
Enter any Number: 496
496 is a Perfect Number
Enter any Number: 56
56 is not a Perfect Number
Comments