Prime Number program in Python


A positive integer number greater than 1 which has no other factors other than 1 and itself is called a prime number. 2, 3, 5, 7, 11, 13 etc. are prime numbers as they do not have any other factors other than 1 and itself.

prime number in python

For Prime Number Algorithm and Pseudocode, you can refer our previous post Algorithm or Pseudocode to find whether a Number is Prime Number or Not

Problem Statement for Below Program

Write a program to check whether a given number is Prime or not in Python

Source Code

##To find whether a number is prime or not ## Prime Number is a number which is divisible only by 1 and itself. def prime(n): if n in (0,1): return "Number is not a prime number" ## A counter to check if the number is divisible or not inside the for loop counter=0 ## Start the loop from 2 to n-1. In range the loop is executed till n-1. Also we have given the start range. for each in range(2, n): if n % each == 0: ## checks if the number is divisible by any other number in this loop. counter += 1 ## If yes counter is incremented. ## If the counter is incremented that means the number is divisible inside the loop. If the counter is zero means it is a Prime Number. if counter == 0: return "Number is a Prime Number" else: return "Number is not a prime Number" if __name__ == '__main__': num = int(raw_input("Enter a number")) result = prime(num) print result


Enter a number 5 Number is a Prime Number


Get Answers to your Programming Questions

Recommended Deals ends in

Quiz For You:

Search Tags