
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Smallest Divisor of an Integer in Python
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
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.
Advertisements