Prime number program in python with explanation

In this tutorial we are going to learn how to write a program to check whether a given integer number by user is a prime number or not in Python programming language.

If given number is prime then our logic will assign value 0 to a temp variable and will print “number is prime” and if the number is not prime then our logic will assign value 1 to a temp variable program will print “number is not prime”.

Before directly moving on the writing prime number program in c, first you should know

In Mathematical term, A prime number is a number which can be divided by only 1 and number itself.

For example : 2, 3, 5, 7, 13,…

Here 2, 3 or any of the above number can only be divided by 1 or number itself.

How our program will behave?

Suppose if someone gives an input 2 then our program should give output “given number is a prime number”.

And if someone gives 4 as  an input then our program should give output “given number is not a prime number”.

Python program to check given number is prime or not

i,temp=0,0
n = int(input("please give a number : "))
for i in range(2,n//2):
    if n%i == 0:
        temp=1
        break
if temp == 1:
    print("given number is not prime")
else:
    print("given number is prime") 

Output:

Python Program to Check Prime Number

Leave a Comment