{"id":10382,"date":"2021-10-01T10:30:22","date_gmt":"2021-10-01T05:00:22","guid":{"rendered":"https:\/\/python-programs.com\/?p=10382"},"modified":"2021-11-22T18:33:28","modified_gmt":"2021-11-22T13:03:28","slug":"python-program-to-print-all-the-prime-numbers-within-a-given-range","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-all-the-prime-numbers-within-a-given-range\/","title":{"rendered":"Python Program to Print all the Prime Numbers within a Given Range"},"content":{"rendered":"
Given the upper limit range, the task is to print all the prime numbers in the given range 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 print all the prime numbers from 2 to the upper limit range some of them are:<\/p>\n If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list<\/a> available.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n All the prime numbers till the upper limit range are printed.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Given the upper limit range, the task is to print all the prime numbers in the given range in Python. Examples: Example1: Input: given number = 95 Output: The prime numbers from 2 to upper limit [ 95 ] : 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 …<\/p>\ngiven number = 95<\/pre>\n
The prime numbers from 2 to upper limit [ 95 ] :\r\n2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89<\/pre>\n
given number = 52<\/pre>\n
Enter some random upper limit range = 52\r\nThe prime numbers from 2 to upper limit [ 52 ] :\r\n2 3 5 7 11 13 17 19 23 29 31 37 41 43 47<\/pre>\n
Python Program to Print all the Prime Numbers within a Given Range<\/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\ngivenNumbr = 95\r\nprint('The prime numbers from 2 to upper limit [', givenNumbr, '] :')\r\n# Set the first for loop to have a range of 2 to the upper limit range.\r\nfor valu in range(2, givenNumbr+1):\r\n # Take a temporary variable which is the count variable and initialize it to zero.\r\n cntvar = 0\r\n # Allow the second for loop to have a range of 2 to\r\n # half the number (excluding 1 and the number itself).\r\n for divi in range(2, valu\/\/2+1):\r\n # Then, using the if statement, determine the number of divisors\r\n # and increment the count variable each time.\r\n if(valu % divi == 0):\r\n cntvar = cntvar+1\r\n # The number is prime if the number of divisors is lower than or equal to zero.\r\n if(cntvar <= 0):\r\n print(valu, end=' ')\r\n<\/pre>\n
The prime numbers from 2 to upper limit [ 95 ] :\r\n2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89<\/pre>\n
Method #2:Using for loop(User Input)<\/h3>\n
\n
# Give the number as user input with the help\r\n# of the int(input()) function and store it in a variable.\r\ngivenNumbr = int(input('Enter some random upper limit range = '))\r\nprint('The prime numbers from 2 to upper limit [', givenNumbr, '] :')\r\n# Set the first for loop to have a range of 2 to the upper limit range.\r\nfor valu in range(2, givenNumbr+1):\r\n # Take a temporary variable which is the count variable and initialize it to zero.\r\n cntvar = 0\r\n # Allow the second for loop to have a range of 2 to\r\n # half the number (excluding 1 and the number itself).\r\n for divi in range(2, valu\/\/2+1):\r\n # Then, using the if statement, determine the number of divisors\r\n # and increment the count variable each time.\r\n if(valu % divi == 0):\r\n cntvar = cntvar+1\r\n # The number is prime if the number of divisors is lower than or equal to zero.\r\n if(cntvar <= 0):\r\n print(valu, end=' ')\r\n<\/pre>\n
Enter some random upper limit range = 52\r\nThe prime numbers from 2 to upper limit [ 52 ] :\r\n2 3 5 7 11 13 17 19 23 29 31 37 41 43 47<\/pre>\n
\n
\n