Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.
Given a number, the task is to find the smallest number which divides the given number in Python.
Examples:
Example1:
Input:
given number = 91
Output:
The smallest which divides the given number [ 91 ] = 7
Example2:
Input:
given number = 169
Output:
Enter some random number = 169 The smallest which divides the given number [ 169 ] = 13
Program to Find the Smallest Divisor of an Integer in Python
There are several ways to find the smallest divisor of the given number in Python some of them are:
- Using for loop(Static Input)
- Using for loop(User Input )
- Using While Loop(Static Input)
- Using While Loop(User Input)
- Using List Comprehension
Method #1:Using for loop(Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Loop from 2 to given number using For loop.
- Check if the iterator value divides the given number perfectly using the if statement and modulus operator.
- Print the iterator value.
- Break the loop using the break statement.
- The Exit of the Program.
Below is the implementation:
# Give the number as static input and store it in a variable. gvnNumb = 91 # Loop from 2 to given number using For loop. for itervalue in range(2, gvnNumb+1): # Check if the iterator value divides the given number # perfectly using the if statement and modulus operator. if(gvnNumb % itervalue == 0): # Print the iterator value. print( 'The smallest which divides the given number [', gvnNumb, '] =', itervalue) # Break the loop using the break statement. break
Output:
The smallest which divides the given number [ 91 ] = 7
Method #2: Using for loop(User Input )
Approach:
- Give the number as user input using the int(input()) function that converts the string to an integer.
- Store it in a variable.
- Loop from 2 to given number using For loop.
- Check if the iterator value divides the given number perfectly using the if statement and modulus operator.
- Print the iterator value.
- Break the loop using the break statement.
- The Exit of the Program.
Below is the implementation:
# Give the number as user input using the int(input()) function that converts # the string to an integer. # Store it in a variable. gvnNumb = int(input('Enter some random number = ')) # Loop from 2 to given number using For loop. for itervalue in range(2, gvnNumb+1): # Check if the iterator value divides the given number # perfectly using the if statement and modulus operator. if(gvnNumb % itervalue == 0): # Print the iterator value. print( 'The smallest which divides the given number [', gvnNumb, '] =', itervalue) # Break the loop using the break statement. break
Output:
Enter some random number = 369 The smallest which divides the given number [ 369 ] = 3
Explanation:
- The user must enter a number.
- The for loop has a range of 2 to the number
- If the remainder of the number is zero when divided by the value of i the element is a divisor of the number.
- Print the number and break the loop.
Method #3:Using While Loop(Static Input)
Approach:
- Give the number as static input and store it in a variable.
- Take a temporary variable and initialize it with 2.
- Loop till the temporary variable is less than or equal to the given number using the while loop.
- Check if the temporary variable divides the given number perfectly using the if statement and modulus operator.
- Print the temporary variable.
- Break the loop using the break statement.
- Increase the value of the temporary variable by 1.
- The Exit of the Program.
Below is the implementation:
# Give the number as static input and store it in a variable. gvnNumb = 125 # Take a temporary variable and initialize it with 2. tempnumber = 2 # Loop till the temporary variable is less than # or equal to the given number using the while loop. while(tempnumber <= gvnNumb): # Check if the temporary variable divides the given number perfectly # using the if statement and modulus operator. if(gvnNumb % tempnumber == 0): # Print the iterator value. print( 'The smallest which divides the given number [', gvnNumb, '] =', tempnumber) # Break the loop using the break statement. break # Increase the value of the temporary variable by 1. tempnumber = tempnumber+1
Output:
The smallest which divides the given number [ 125 ] = 5
Method #4:Using While Loop(User Input)
Approach:
- Give the number as user input using the int(input()) function that converts the string to an integer.
- Store it in a variable.
- Take a temporary variable and initialize it with 2.
- Loop till the temporary variable is less than or equal to the given number using the while loop.
- Check if the temporary variable divides the given number perfectly using the if statement and modulus operator.
- Print the temporary variable.
- Break the loop using the break statement.
- Increase the value of the temporary variable by 1.
- The Exit of the Program.
Below is the implementation:
# Give the number as user input using the int(input()) function that converts # the string to an integer. # Store it in a variable. gvnNumb = int(input('Enter some random number = ')) # Take a temporary variable and initialize it with 2. tempnumber = 2 # Loop till the temporary variable is less than # or equal to the given number using the while loop. while(tempnumber <= gvnNumb): # Check if the temporary variable divides the given number perfectly # using the if statement and modulus operator. if(gvnNumb % tempnumber == 0): # Print the iterator value. print( 'The smallest which divides the given number [', gvnNumb, '] =', tempnumber) # Break the loop using the break statement. break # Increase the value of the temporary variable by 1. tempnumber = tempnumber+1
Output:
Enter some random number = 578 The smallest which divides the given number [ 578 ] = 2
Method #5:Using List Comprehension
Approach:
- Give the number as user input using the int(input()) function that converts the string to an integer.
- Store it in a variable.
- Using List Comprehension, for loop and if statements add all the divisors of the given number from 2 to given number into the list.
- Print the first element in the given list.
- The Exit of the Program.
Below is the implementation:
# Give the number as user input using the int(input()) function that converts # the string to an integer. # Store it in a variable. gvnNumb = int(input('Enter some random number = ')) # Using List Comprehension, for loop and if statements add # all the divisors of the given number # from 2 to given number into the list. divList = [numbe for numbe in range(2, gvnNumb+1) if(gvnNumb % numbe == 0)] smalldivi = divList[0] # Print the first element in the given list. print('The smallest which divides the given number [', gvnNumb, '] =', smalldivi)
Output:
Enter some random number = 169 The smallest which divides the given number [ 169 ] = 13
Related Programs:
- Python Program to Form an Integer that has the Number of Digits at Ten’s Place and the Least Significant Digit of the Entered Integer at One’s Place
- Python Program to Find the Factorial of a Number
- Python Program to Find the Sum of Natural Numbers
- Python Program to Find the Factors of a Number
- Python Program to Find the LCM of two Numbers
- Python Program to Find the Missing Number in an array/list
- Python Program to Find the Odd Occurring Element in an Array/list