{"id":10374,"date":"2021-09-30T11:00:23","date_gmt":"2021-09-30T05:30:23","guid":{"rendered":"https:\/\/python-programs.com\/?p=10374"},"modified":"2021-11-22T18:35:32","modified_gmt":"2021-11-22T13:05:32","slug":"python-program-to-find-the-smallest-divisor-of-an-integer","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-smallest-divisor-of-an-integer\/","title":{"rendered":"Python Program to Find the Smallest Divisor of an Integer"},"content":{"rendered":"
Our website provided core java programs examples with output<\/a> aid beginners and expert coders to test their knowledge gap and learn accordingly.<\/p>\n Given a number, the task is to find the smallest number which divides the given number in Python.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to find the smallest divisor of the given number in Python some of them are:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\ngiven number = 91<\/pre>\n
The smallest which divides the given number [ 91 ] = 7<\/pre>\n
given number = 169<\/pre>\n
Enter some random number = 169\r\nThe smallest which divides the given number [ 169 ] = 13<\/pre>\n
Program to Find the Smallest Divisor of an Integer in Python<\/h2>\n
\n
Method #1:Using for loop(Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvnNumb = 91\r\n# Loop from 2 to given number using For loop.\r\nfor itervalue in range(2, gvnNumb+1):\r\n # Check if the iterator value divides the given number\r\n # perfectly using the if statement and modulus operator.\r\n if(gvnNumb % itervalue == 0):\r\n # Print the iterator value.\r\n\r\n print(\r\n 'The smallest which divides the given number [', gvnNumb, '] =', itervalue)\r\n # Break the loop using the break statement.\r\n break\r\n<\/pre>\n
The smallest which divides the given number [ 91 ] = 7<\/pre>\n
Method #2: Using for loop(User Input )<\/h3>\n
\n
# Give the number as user input using the int(input()) function that converts\r\n# the string to an integer.\r\n# Store it in a variable.\r\ngvnNumb = int(input('Enter some random number = '))\r\n# Loop from 2 to given number using For loop.\r\nfor itervalue in range(2, gvnNumb+1):\r\n # Check if the iterator value divides the given number\r\n # perfectly using the if statement and modulus operator.\r\n if(gvnNumb % itervalue == 0):\r\n # Print the iterator value.\r\n\r\n print(\r\n 'The smallest which divides the given number [', gvnNumb, '] =', itervalue)\r\n # Break the loop using the break statement.\r\n break\r\n<\/pre>\n
Enter some random number = 369\r\nThe smallest which divides the given number [ 369 ] = 3<\/pre>\n
\n
Method #3:Using While Loop(Static Input)<\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvnNumb = 125\r\n# Take a temporary variable and initialize it with 2.\r\ntempnumber = 2\r\n# Loop till the temporary variable is less than\r\n# or equal to the given number using the while loop.\r\nwhile(tempnumber <= gvnNumb):\r\n # Check if the temporary variable divides the given number perfectly\r\n # using the if statement and modulus operator.\r\n if(gvnNumb % tempnumber == 0):\r\n # Print the iterator value.\r\n print(\r\n 'The smallest which divides the given number [', gvnNumb, '] =', tempnumber)\r\n # Break the loop using the break statement.\r\n break\r\n # Increase the value of the temporary variable by 1.\r\n tempnumber = tempnumber+1\r\n<\/pre>\n
The smallest which divides the given number [ 125 ] = 5<\/pre>\n
Method #4:Using While Loop(User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function that converts\r\n# the string to an integer.\r\n# Store it in a variable.\r\ngvnNumb = int(input('Enter some random number = '))\r\n# Take a temporary variable and initialize it with 2.\r\ntempnumber = 2\r\n# Loop till the temporary variable is less than\r\n# or equal to the given number using the while loop.\r\nwhile(tempnumber <= gvnNumb):\r\n # Check if the temporary variable divides the given number perfectly\r\n # using the if statement and modulus operator.\r\n if(gvnNumb % tempnumber == 0):\r\n # Print the iterator value.\r\n print(\r\n 'The smallest which divides the given number [', gvnNumb, '] =', tempnumber)\r\n # Break the loop using the break statement.\r\n break\r\n # Increase the value of the temporary variable by 1.\r\n tempnumber = tempnumber+1\r\n<\/pre>\n
Enter some random number = 578\r\nThe smallest which divides the given number [ 578 ] = 2<\/pre>\n
Method #5:Using List Comprehension<\/h3>\n
\n
# Give the number as user input using the int(input()) function that converts\r\n# the string to an integer.\r\n# Store it in a variable.\r\ngvnNumb = int(input('Enter some random number = '))\r\n# Using List Comprehension, for loop and if statements add\r\n# all the divisors of the given number\r\n# from 2 to given number into the list.\r\ndivList = [numbe for numbe in range(2, gvnNumb+1) if(gvnNumb % numbe == 0)]\r\nsmalldivi = divList[0]\r\n# Print the first element in the given list.\r\nprint('The smallest which divides the given number [', gvnNumb, '] =', smalldivi)\r\n<\/pre>\n
Enter some random number = 169\r\nThe smallest which divides the given number [ 169 ] = 13<\/pre>\n
\n