Q:

Write a python program to display all prime numbers within a range

belongs to collection: Python Loop Exercises

0

Write a program to display all prime numbers within a range

Note: A Prime Number is a number that cannot be made by multiplying other whole numbers. A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers

Examples:

  • 6 is not a prime mumber because it can be made by 2×3 = 6
  • 37 is a prime number because no other whole numbers multiply together to make it.

Given:

# range
start = 25
end = 50

Expected output:

Prime numbers between 25 and 50 are:
29
31
37
41
43

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Solution:

start = 25
end = 50
print("Prime numbers between", start, "and", end, "are:")

for num in range(start, end + 1):
    # all prime numbers are greater than 1
    # if number is less than or equal to 1, it is not prime
    if num > 1:
        for i in range(2, num):
            # check for factors
            if (num % i) == 0:
                # not a prime number so break inner loop and
                # look for next number
                break
        else:
            print(num)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Display Fibonacci series up to 10 terms using pyth... >>
<< Use else block to display a message “Done” aft...