Python Program to Find Prime Numbers in a Given Range

This is a Python Program to print all prime numbers within a given range.

Problem Description

The program takes in the upper limit and prints all prime numbers within the given range.

Problem Solution

1. Take in the upper limit for the range and store it in a variable.
2. Let the first for loop range from 2 to the upper limit.
3. Initialize the count variable to 0.
4. Let the second for loop range from 2 to half of the number (excluding 1 and the number itself).
5. Then find the number of divisors using the if statement and increment the count variable each time.
6. If the number of divisors is lesser than or equal to 0, the number is prime.
7. Print the final result.
8. Exit.

Program/Source Code

Here is source code of the Python Program to print all the prime numbers within a given range. The program output is also shown below.

r=int(input("Enter upper limit: "))
for a in range(2,r+1):
    k=0
    for i in range(2,a//2+1):
        if(a%i==0):
            k=k+1
    if(k<=0):
        print(a)
Program Explanation

1. User must enter the upper limit of the range and store it in a different variable.
2. The first for loop ranges till the upper limit entered by the user.
3. The count variable is first initialized to 0.
4. The for loop ranges from 2 to the half of the number so 1 and the number itself aren’t counted as divisors.
5. The if statement then checks for the divisors of the number if the remainder is equal to 0.
6. The count variable counts the number of divisors and if the count is lesser or equal to 0, the number is a prime number.
7. If the count is greater than 0, the number isn’t prime.
8. The final result is printed.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter upper limit: 15
2
3
5
7
11
13
 
Case 2:
Enter upper limit: 40
2
3
5
7
11
13
17
19
23
29
31
37

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.