Python Program to Find the Smallest Divisor of an Integer


When it is required to find the smallest divisor of a an integer, a simple ‘for’ loop is used.

Below is a demonstration of the same −

Example

 Live Demo

first_num = int(input("Enter a number..."))
my_list = []

print("The number is ")
print(first_num)

for i in range(2,first_num+1):
   if(first_num%i==0):
      my_list.append(i)
my_list.sort()
print("The smallest divisor is : ")
print(my_list[0])

Output

Enter a number...56
The number is
56
The smallest divisor is :
2

Explanation

  • The number is taken as an input from the user.

  • An empty list is defined.

  • The number taken from user is displayed on the console.

  • The number range is iterated over.

  • It is checked to see if the number divided by the iterator is 0.

  • If yes, it is appended to the empty list.

  • In the end, this list is sorted.

  • The first element of the sorted list is displayed on the console, since this is the smallest divisor.

Updated on: 16-Apr-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements