Write a program that asks the user to enter a number and prints the sum of the divisors of
that number. The sum of the divisors of a number is an important function in number theory.
# get the number from the user
number = int(input("Enter a number: "))
# initialize the sum of the divisors to 0
divisor_sum = 0
# loop through the numbers from 1 to the number
for i in range(1, number+1):
# check if i is a divisor of the number
if number % i == 0:
# if it is, add it to the sum of the divisors
divisor_sum += i
# print the sum of the divisors
print(divisor_sum)
This program first gets the number from the user and initializes a divisor_sum
variable to 0. It then loops through the numbers from 1 to the number, and checks if each number is a divisor of the given number. If it is, it adds it to the divisor_sum
. Finally, it prints the divisor_sum
.
If you run the program, it will ask you to enter a number, and then it will compute and print the sum of the divisors of that number. For example, if you enter 12
when prompted, you might see something like:
28
0 Comments