Python program to print prime numbers in an interval

In this post, we will print prime numbers in an interval. It is extension of how to check if number is prime or not.

A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.


Problem

Given the min and max, print all the prime numbers in the range

Program to prime number in an interval

import math def isPrime(num): for i in range(2,int(num**0.5)): if(num%i==0): return False return True def printPrimeNumbers(min,max): for i in range(min,max+1): if(isPrime(i)): print(i) # Prime numbers in range of 10 to 30 printPrimeNumbers(10,20)

Output:

11
13
15
17
19

We have defined two functions(i.e. isPrime and printPrimeNumbers)
isPrime: This function checks if number is prime or not.If it is prime, it will retrun true else will return false.
printPrimeNumbers: : This function iterates over range from min to max+1, and checks each number in the range if it is prime or not. if it is prime, it will print it else will skip it.

That’s all about printing prime numbers in an interval.

Leave a Reply

Your email address will not be published. Required fields are marked *