Skip to main content
Check Prime Number in Python With Flowchart

Check Prime Number in Python With Flowchart

As programmers, we are frequently required to train algorithms. This is not done in vain. This exercise can help us find solutions to problems in the future.

So this time, we will train our algorithm today using a simple case study. That is check whether or not a number is a prime number. This case study will be completed using the python programming language and its flowchart.

Overview of Troubleshooting

There are a few things you should know before moving on to the flowchart and code creation stages. Specifically, what is a prime number and how to tell if a number is a prime number or not. Once we know it, we can show whether or not the number is a prime number.

So, what are prime numbers? “A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers.”, according to Wikipedia. A prime number is a number that has only two factors or can only be divided by two numbers, namely the number 1 and the number itself. For example, if the number 5 is divided by any number, it will produce a comma or decimal number unless it is divided by the number 1 and the number itself. As a result, the number 5 is known as a prime number because it has only two factors or can only be divided by two numbers.

Overview of Prime Number
Overview of Prime Number

To determine if a number is a prime number, divide it by the number below it one by one. If the result of the division does not contain a comma, the divisor is a factor of the number being divided.


Related post: Find Prime Numbers in Range Python With Flowchart


Flowchart to Check Prime Number

After we have a general understanding of troubleshooting from our case study, we will create a flowchart. The program’s flowchart displays prime numbers as follows:

Check Prime Number Flowchart
Check Prime Number Flowchart
  1. First, we will store the number entered by the user in n variable and then determine whether or not it is a prime number.
  2. We make a variable called factor_count with an initial value of 0. This variable will be used later to store the number of factors of the number entered by the user.
  3. We will use the iteration variable in the form of i variable as the divisor of variable n in this for loop from 1 to n.
  4. Then we check to see if n mod i equals 0. This mod computes the remainder of n variable divided by i variable.
  5. If the condition in point 4 holds, i variable is now a factor of n variable. As a result, we will increment the factor_count variable by one which means increasing the number of factors in the n variable.
  6. Following the completion of the loop, we will check whether the number of factors (factor_count variable) is equal to 2, which is used to determine whether the number entered by the user (i variable) is a prime number or not.
  7. If condition point 6 is true, the output will be “n is a prime number”. If the condition is false, it will display “n is not a prime number”.

Related post: Python Operators With Examples


Let’s Code Using Python

Making a flowchart, as shown above, allows us to determine the flow of the program that we will create. Making program code becomes easier in this manner. Based on the flowchart, we will write program code to check prime number using python in this section. The generated program code is as follows:

n = int(input("Enter a number: "))
factor_count = 0

for i in range(1, n + 1):
    if n % i == 0:
        factor_count += 1

if factor_count == 2:
    print(n, "is a prime number")
else:
    print(n, "is not a prime number")

That’s how to use the python programming language to check prime number. Even though the process described above appears to be time-consuming, it actually makes it easier for us to code. Making a flowchart first can help us get used to creating an overview that will come in handy later.

Thank you for taking the time to read this. I hope you found it useful.

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.

One thought to “Check Prime Number in Python With Flowchart”

  1. Good post. I learn something new and challenging on websites I stumbleupon everyday.
    It will always be exciting to read content from other writers
    and practice a little something from other
    websites.

Leave a Reply

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