Python Program to printing all divisors of a number in a range


April 4, 2022, Learn eTutorial
1151

In this simple python program, we need to find all the divisors of a number. It's an arithmetic operation-based python program.

To understand this example, you should have knowledge of the following Python programming topics:

How to get all divisors of a number in a range using python?

A simple python program to display all divisors of a number, which means printing all the numbers which are divisible by a given number in some range. To check a number is divisible by a given number by checking the remainder is zero. If the remainder is zero after the division, then it is divisible.

For example, let us take the number 5 and check the number 15 is divisible by 5. 15 / 5 And the resulting remainder is zero, so it is divisible.

To apply this simple logic in python language, we take the values of the lower and upper limit from the user. Then the number is checked if it is divisible or not. For that, we open an for loop iterating each number from the lower range to the upper range. Now check the selected number is divisible by the given number using the mod operator inside an if condition in python and print the number.

ALGORITHM

STEP 1: Accept the lower and upper limit values from the user using the input method and convert that string to an integer using int() in python programming.

STEP 2: Accept the value of n, the number that to be divided by the numbers in the given range.

STEP 3: Open a for loop from lower to upper using the range method in python.

STEP 4: Check the reminder of each number in the range that is zero or not using an if condition and mod operator. if the condition matches then print the number.

Python Source Code

                                          lower=int(input("Enter lower range limit:"))

upper=int(input("Enter upper range limit:"))

n=int(input("Enter the number to be divided by:"))

for i in range(lower,upper+1):

    if(i%n==0):

        print(i)
                                      

OUTPUT

Enter lower range limit: 10

Enter upper range limit: 25

Enter the number to be divided by: 5

10
15
20
25