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

**Printing perfect numbers**: Here, we are going to learn **how to find and print the perfect numbers from a given list in Python?**

Submitted by Ankit Rai, on July 26, 2019

Given a list of the integer numbers and we have to **print all perfect numbers present in the given list**.

This Program iterating through each number one by one in the list, and **check whether a given number is a perfect number or not**. If a perfect number is found then print it else skip it.

In this program, **checkPerfectNum()** function is used to find its all positive divisors excluding that number and sum it all and then check for perfect number condition.

**Explanation:** For Example, 28 is a perfect number since divisors of 28 are 1, 2, 4,7,14 then sum of its divisor is 1 + 2 + 4 + 7 + 14 = 28.

**Note:** A perfect number is a positive integer that is equal to the sum of its proper positive divisors.

### Python code to print perfect numbers from the given list of integers

# 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

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.