# Python Program to Check Perfect Number

« Previous Program Next Program »

• Check perfect number using for loop
• Check perfect number using while loop

### What is a Perfect Number ?

A perfect number is a number that is equal to the sum of its positive divisors, excluding the number itself. That is, the number 6 has divisors 1, 2, and 3 (excluding 6 itself). And 1+2+3 is equal to 6. Therefore 6 is a perfect number.

## Check Perfect Number using for Loop

The question is, write a program in Python that checks whether a number is a perfect or not using for loop. Here is its answer:

```print("Enter the Number:")
num = int(input())
sum = 0
for i in range(1, num):
if num%i==0:
sum = sum+i
if num==sum:
print("It is a Perfect Number")
else:
print("It is not a Perfect Number")```

Here is its sample run:

Now supply the input say 6 and press `ENTER` key to check and print whether it is a perfect number or not as shown in the snapshot given below:

#### Modified Version of Previous Program

This is the modified version of previous program, uses end= and str(). Rest of the things are almost similar to previous program. The end= is used to skip printing of an automatic newline. And the str() converts any type into a string type.

```print(end="Enter the Number: ")
num = int(input())
sum = 0
for i in range(1, num):
if num%i==0:
sum = sum+i
if num==sum:
print("\n" + str(num) + " is a Perfect Number")
else:
print("\n" + str(num) + " is not a Perfect Number")```

Here is its sample run with user input 496:

## Check Perfect Number using while Loop

The question is, write a Python program to check for perfect number using while loop. Here is its answer:

```print(end="Enter the Number: ")
num = int(input())
sum = 0
i = 1
while i<num:
if num%i==0:
sum = sum+i
i = i+1
if num==sum:
print("\n" + str(num) + " is a Perfect Number")
else:
print("\n" + str(num) + " is not a Perfect Number")```

Python Online Test

« Previous Program Next Program »
{"ft_lang_label":"__label__en","ft_lang_prob":0.81501627,"math_prob":0.99581045,"size":1789,"snap":"2021-31-2021-39","text_gpt3_token_len":476,"char_repetition_ratio":0.14453782,"word_repetition_ratio":0.29819277,"special_character_ratio":0.2962549,"punctuation_ratio":0.110539846,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9994789,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-09-21T16:37:59Z\",\"WARC-Record-ID\":\"<urn:uuid:27cf2a4c-5894-428f-93f8-f96eb061ea3e>\",\"Content-Length\":\"30089\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fd8abf01-7928-4ba9-a774-12707d04eb9a>\",\"WARC-Concurrent-To\":\"<urn:uuid:fdfa57ca-908c-4a8e-b0b9-99db582e4bf0>\",\"WARC-IP-Address\":\"148.72.215.147\",\"WARC-Target-URI\":\"https://codescracker.com/python/program/python-check-perfect-number.htm\",\"WARC-Payload-Digest\":\"sha1:LIRM7ZQHE7VZI5VYQNBEKSLVBW4YXVZF\",\"WARC-Block-Digest\":\"sha1:3ADLIBQWQEQCABCTVFHRQS2A42P2IZOI\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-39/CC-MAIN-2021-39_segments_1631780057225.57_warc_CC-MAIN-20210921161350-20210921191350-00383.warc.gz\"}"}