Skip to main content
Find Prime Numbers in Range Python With Flowchart

Find Prime Numbers in Range Python With Flowchart

A prime number is a number with only two factors. A prime number’s factors are the number 1 and the number itself. Prime numbers include 2, 3, 5, 7, and so on. But not with the number 6, because it has four factors: 1, 2, 3, and 6.

On this occasion, we will use a simple case study to train our algorithm. The case study involves using Python and its flowchart to find prime numbers in a range or interval.

Before we proceed, I hope you are all familiar with prime numbers. The complete explanation can be found on the Wikipedia page.

Flowchart Find Prime Numbers in Range

The first step is to create a flowchart. Making a flowchart can help you see the structure of the algorithm more clearly. So that we can write program code more easily later on.

Flowchart Find Prime Numbers in Range
flowchart find prime numbers in range

Explanation:

  1. First, we collect user input to determine the starting and ending numbers (intervals). The start variable stores the initial number, and the end variable stores the final number.
  2. Then we do a loop from the first to the last number.
  3. We look to see if the iteration variable from point 2 is greater than one. This is done to prevent bugs in the program if the user enters a negative number as the initial value. While prime numbers contain positive numbers.
  4. Then we make a factors variable with a value of 0. This variable will later be used to store a number’s many factors.
  5. We are currently looping from 1 to i + 1. This loop has an iteration variable that will be used to determine the factors later on.
  6. We check whether i variable modulus j variable equals 0. If the conditions are met, add the factors variable by 1. j variable is therefore a factor of i variable.
  7. We will proceed to the next step once the loop to determine the factors in point 5 is completed. Here, we will see if the factors variable equals 2. This implies that the i variable has only two factors.
  8. If the conditions in point 7 are met, the output of i variable is displayed. i variable in other words, is a prime number. If the conditions are not met, the loop is resumed at point 2.

Related post: Check Prime Number in Python With Flowchart


Program Code Find Prime Numbers in Range

It is now time for us to write program code in Python. Based on the previously created flowchart, the program code is as follows:

start = int(input("Enter start value: "))
end = int(input("Enter end value: "))

for i in range(start, end + 1):
    if i > 1:
        factors = 0

        for j in range(1, i + 1):
            if i % j == 0:
                factors += 1

        if factors == 2:
            print(i)

Output:

Enter start value: 10
Enter end value: 20
11
13
17
19

Related post: Python Operators With Examples


Conclusion

In Python, this is how you find prime numbers in a range or interval. If you have any questions, please leave them in the comments section.

Hopefully, this will help to train our algorithm, which will be useful in the future. Thank you very much.

Share this

Shafy Gunawan

I’m a web developer. I spend my whole day, practically every day, experimenting with HTML, CSS, JavaScript, and PHP; dabbling with Python programming language; and share the knowledge that I learned.

Leave a Reply

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