Hello people, here I am writing a Python programme after a very long time. This programme could be useful for cracking interviews or could be useful for school / college students who wish to learn the basic of Python programming.
This programme basically gets an input value from the user and prints whether the input number is either a Prime Number or Not a Prime Number
I hope you know what is a Prime Number already. If you don’t know here is a simple explanation:
A prime number should be exactly #divisible by 2 times only (itself and 1)
Find Prime number – Python program :
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
def find_prime(num): res = False c = 0 i = 1 #loop till i equals to num while i <= num: #% modules will give the reminder value, #so if the reminder is 0 then it is divisible if (num % i == 0): #increment the value of c c = c + 1 #increment i i = i + 1 #if the value of c is 2 then it is a prime number #because a prime number should be exactly #divisible by 2 times only (itself and 1) if c == 2: res = True return res #get a input value print "Enter a Number" num = int(input()) #if find_prime() returns True then it's a Prime if find_prime(num): print "It's a Prime Number!" else: print "Not a Prime Number!" |
Read the comments in the above program to understand. Just copy paste and execute the above snippet to get the output.
Enjoy!
Pingback: Python Program to print Prime numbers - TutorialsMade