Python program to print perfect numbers from the given list of integers

Printing perfect numbers: Here, we will figure out how to discover and print the perfect numbers from a given rundown in Python?

Given a rundown of the whole number numbers and we need to print every perfect number present in the given rundown.

This Program emphasizing through each main by one in the rundown, and check whether a given number is a perfect number or not. On the off chance that a perfect number is discovered, at that point print, it else skips it.

In this program, checkPerfectNum() work is utilized to locate its everything positive divisors barring that number and aggregate everything and afterwards check for perfect number condition.

Clarification: For Example, 28 is a perfect number since divisors of 28 are 1, 2, 4,7,14 at that point total of its divisor is 1 + 2 + 4 + 7 + 14 = 28.

Note: A perfect number is a positive whole number that is equivalent to the aggregate of its appropriate positive divisors.

Python code to print perfect numbers from the given rundown of whole numbers:

# Define a function for checking perfect number
# and print that number
def checkPerfectNum(n) :
	# initialisation 
	i = 2;sum = 1;

	# iterating till n//2 value
	while(i <= n//2 ) :
		# if proper divisor then add it.
		if (n % i == 0) :
			sum += i			
		
		# incrementing i by one
		i += 1
		
		# check sum equal to n or not
		if sum == n :
			print(n,end=' ')

# Main code
if __name__ == "__main__" :

	# take list of number as an input from user 
	# and typecast into integer
	print("Enter list of integers: ")
	list_of_intgers = list(map(int,input().split()))

	print("Given list of integers:",list_of_intgers)

	print("Perfect numbers present in the list is: ")
	# Iteration through the each element of 
	# the list one by one
	for num in list_of_intgers :
		# function call
		checkPerfectNum(num)

Output:

Enter list of integers:
14 20 6 78 28
Given list of integers: [14, 20, 6, 78, 28]
Perfect numbers present in the list is:
6 28

Leave a Comment

error: Alert: Content is protected!!