Prime Number in Python

In this tutorial, we are going to see the code of a prime number in Python to check whether a given number is a prime number or not

What is a Prime Number?

A prime number is a number greater than 1 , which is not divisible by any other number except 1 and itself. For example 2, 3,5,7,11 etc.

Python Code to check Prime Number

steps to check prime number

  • Take a number input from user
  • First check if number is less then 2, then it’s not a prime number
  • else run a loop from i =0 to i =number and create a boolean variable and initializa it true
  • check if the number is divisible by i, if yes then the number is not a prime number, update the boolean variable to false and break the loop
  • end of loop
  • if the boolean variable is true, then the number is a prime number

Python Prime Number Code

number = int(input("enter the number "))

if number<2:
    print("this is not a prime number")
else:
    status = True
    for i in range(2,number):
        if (number%i == 0):
            status = False
            print("this is not a prime number")
            break
        
    if status:
        print("this is a prime number")
    else:
        print("this is not a prime number")    

Output

Prime Number in Python

thanks, for more Python and java tutorial you can check https://gangforcode.com/

6 thoughts on “Prime Number in Python”

Leave a Comment