Exercise 11

Write a Python algorithm that asks the user to input an integer and then displays all of its divisors.

Solution

n = int(input("Enter an integer: "))

print("The divisors of", n, "are:")
for i in range(1, n+1):
    if n % i == 0:
        print(i)





In this program:

  1. We first prompts the user to enter an integer n: and stores it in the variable n.
  2. Then we enters a for loop: that iterates from 1 to n (inclusive),
  3. Then we check if n: is divisible by each value of i in the loop.
  4. If it is: the program prints the value of i, which is a divisor of n.

 

Younes Derfoufi
my-courses.net

One thought on “Solution Exercise 11: python algorithm that determines all divisors of given number”

Leave a Reply