# Python Program to Check Prime Number

In this post, we will learn how to check prime numbers using Python Programming language.

A prime number is a whole number greater than 1, and has exactly two factors that is 1 and the number itself. For example: 2, 3, 5, 7, 11, 13, 17, . . . and so on.

We will be using following approaches to check prime number:

1. Using a flag Variable
2. Using If-Else Statement

So, without further ado, let’s begin this tutorial.

Contents

## Python Program to Check Prime Number

```# Python Program to Check Prime Number
num = int(input("Enter a number: "))

# defining a flag variable
flag = 1

# Checking for factors
for i in range(2, int (num/2)):
if num % i == 0:
flag = 0
break

# Display output
if flag == 1 and num >= 2:
print(num, "is a prime number.")
else:
print(num, "is not a prime number.")
```

Output 1

``````Enter a number: 17
17 is a prime number.
``````

Output 2

``````Enter a number: 25
25 is not a prime number.
``````

## How Does This Program Work ?

```num = int(input("Enter a number: "))
```

The user is asked to enter a number. The entered number gets stored in the num named variable.

```# defining a flag variable
flag = 1
```

We define a variable named flag. Flag variable has assigned a value of 1.

```# Checking for factors
for i in range(2, int (num/2)):
if num % i == 0:
flag = 0
break
```

Then, we check whether there is any factor of num greater than 2. If yes, then the flag becomes 0 and the loop terminates.

```if flag == 1 and num >= 2:
print(num, "is a prime number.")
else:
print(num, "is not a prime number.")
```

If the value of the flag is 1 and num >= 2, then in this case the entered number doesn’t have any factors greater than 2 so, it is a prime number otherwise the entered number is not a prime number.

## Python Program to Check Prime Number Using If Else Statement

```# Python Program to Check Prime Number Using If-Else Statement
num = int(input("Enter a number: "))

if num > 1:
for i in range(2, num):
if (num % i == 0):
print(num, 'is not a prime number.')
break
else:
print(num, 'is a prime number.')

# If entered number is less than 1, then it is not a prime number
else:
print(num, "is not a prime number.")
```

Output 1

``````Enter a number: 13
13 is a prime number.
``````

Output 2

``````Enter a number: 91
91 is not a prime number.
``````

## Conclusion

I hope after going through this post, you understand how to check prime numbers using Python Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to assist you.