
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to Check if a Number is a Perfect Number
A number is said to be a Perfect Number when that is equal to the sum of all its positive divisors except itself. When it is required to check if a number is a perfect number, a simple ‘for’ loop can be used.
Below is the demonstration of the same −
Example
n = 6 my_sum = 0 for i in range(1, n): if(n % i == 0): my_sum = my_sum + i if (my_sum == n): print("The number is a perfect number") else: print("The number is not a perfect number")
Output
The number is a perfect number
Explanation
The value for ‘n’ is specified.
The sum is initialized to 0.
The number is iterated over, and the sum is incremented.
If this sum is equal to the previously defined ‘n’, it is considered a perfect number.
Relevant messages are displayed on the console.
- Related Articles
- Golang Program to Check if a Number is a Perfect Number
- Python Program to Check if a Number is a Strong Number
- Check if a number in a list is perfect square using Python
- Swift Program to Check if the given number is Perfect number or not
- Check if given number is perfect square in Python
- Python program to check if the given number is a Disarium Number
- How to check if a given number is a Fibonacci number in Python Program ?
- Python Program for How to check if a given number is a Fibonacci number?
- Check if the given number is a perfect cube: $216$.
- Swift Program to Check whether a number is a Perfect Cube or not
- C Program to check Plus Perfect Number
- Python program to check if a given string is number Palindrome
- Python program to check if a number is Prime or not
- Python program to check if the given number is Happy Number
- Python Program to Check if a Number is Positive, Negative or 0

Advertisements