Prime Numbers in Range

Write a Python program that takes in two positive integers (a, b) as input and prints all the prime numbers within that inclusive range.

Example 1:

Input: 2, 10 

Output: 2, 3, 5, 7

Example 2:

Input: 11, 20 

Output: 11, 13, 17, 19

A number is prime if it has no other divisors other than 1 and itself. You need to check this for each number in the range.

def print_primes_in_range(a, b):
    for num in range(a, b + 1):
        if num > 1:
            for i in range(2, num):
                if (num % i) == 0:
                    break
            else:
                print(num)

print_primes_in_range(2, 10)

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!