write a python program to check if a given number is a perfect number or not.
Create a function to check if a given number is a perfect number or not.
I completed my post-graduation in 2013 in the engineering field. Engineering is the application of science and math to solve problems. Engineers figure out how things work and find practical uses for scientific discoveries. Scientists and inventors often get the credit for innovations that advance the human condition, but it is engineers who are instrumental in making those innovations available to the world. I love pet animals such as dogs, cats, etc.
Aryan Kumar
19-Jun-2023Sure, here is a Python program to check if a given number is a perfect number or not:
Python
This program works by first initializing a variable called
sum_of_factorsto 1. Then, it iterates through the numbers from 2 tonumberand adds each factor ofnumbertosum_of_factors. If the sum of the factors is equal tonumber, then the number is a perfect number. Otherwise, the number is not a perfect number.To run the program, you can save it as a Python file and then run it from the command line. For example, if you save the program as
is_perfect_number.py, you can run it by typing the following command into the command line:Code snippet
This will print the boolean value of whether the number is a perfect number to the console.
Here is an example of the output of the program:
Code snippet
As you can see, the output of the program is True, which means that the number is a perfect number.