Python Program to find smallest divisor of a number


February 12, 2022, Learn eTutorial
1505

What is a divisor?

The divisor is an integer that can divide a number completely without any remainder. As we know any number can be divided by 1 so we are excluding 1 from the divisor list, and starting from 2 only.

For example, let us take the number 12 and the divisors of 12 are 2, 3, 4, 6, and 12. In this python program, we are finding the lowest divisor using mod operatorfor loop and if condition.

Note: For a prime number like 13 17, the smallest divisor will be the number itself.

How to find the smallest divisor in python?

First, we open a  for loop from 2 to that number and check each integer in the for loop iteration using the Mod operator. After each iteration of for loop we have to check the number mod i is zero using the if condition, If we got the condition true while the iteration then print i is the smallest divisor and break the loop.

ALGORITHM

STEP 1: Accept input num from the user using the input function and convert the string to an integer using int() in python language.

STEP 2: Use a for loop from i=2 to num+1, check the user input is divisible by i; if yes print i is the smallest divisor and break the loop


For finding the lowest divisor, we are using the below python topics, please refer to those for a better understanding

Python Source Code

                                          num=int(input("Enter a number:"))

for i in range(2,num+1):
    if(num%i==0):
        print("The smallest divisor is:",i)   # print the Smallest divisor
        break
                                      

OUTPUT

Enter a number: 15

The smallest divisor is: 3