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:
- We first prompts the user to enter an integer n: and stores it in the variable n.
- Then we enters a for loop: that iterates from 1 to n (inclusive),
- Then we check if n: is divisible by each value of i in the loop.
- If it is: the program prints the value of i, which is a divisor of n.
Younes Derfoufi
my-courses.net
[…] Exercise 11 || Solution […]