119k views
1 vote
Write an application that displays every perfect number from 1 through 1,000. A perfect number is one that equals the sum of all the numbers that divide evenly into it. For example, 6 is perfect because 1, 2, and 3 divide evenly into it, and their sum is 6; however, 12 is not a perfect number because 1, 2, 3, 4, and 6 divide evenly into it, and their sum is greater than 12.

1 Answer

5 votes

Answer:

Written in Python

for num in range(1,1001):

sum=0

for j in range(1,num+1):

if num%j==0:

sum = sum + j

if num == sum:

print(str(num)+" is a perfect number")

Step-by-step explanation:

This line gets the range from 1 to 1000

for num in range(1,1001):

This line initializes sum to 0 for each number 1 to 1000

sum=0

This line gets the divisor for each number 1 to 1000

for j in range(1,num+1):

This following if condition line checks for divisor of each number

if num%j==0:

sum = sum + j

The following if condition checks for perfect number

if num == sum:

print(str(num)+" is a perfect number")

User Om Rastogi
by
5.3k points