Table of Contents
Python code to check whether the number is prime or not
In this tutorial, we will discuss the Python code to check whether the number is prime or not
In this post, we are going to learn how to check whether the given number is prime or not using 4 ways in Python language
This is done using for loop, while loop function and recursion
A prime number is a number which is greater than(positive) one and divisible by only two numbers: 1 and it self. when any number is divisible by any other number it is not a prime number.
So, prime numbers can,t be divided by other numbers than itself or 1 Eg 2,3,5,7,11,13,17 ………..
Code to check whether the number is prime or not
Code to check whether the number is prime or not – using for loop
Program 1
#Python program to check prime or not #Taking input from the user num=int(input("Enter any number: ")) #check the prime number using num>1 condition 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 the given number is less than or equal to 1 else: print(num," is not a prime number")
When the above code is executed, it produces the following result
Case 1
Enter any number: 13 13 is a prime number'
Case 2
Enter any number: 40 40 is not a prime number'
Case 3
Enter any number: 1 1 is not a prime number'
Code to check whether the number is prime or not – using while loop
Program 2
#Python program to check prime number num=int(input("Please enter a number: ")) count=0 i=2 while(i<=num//2): if(num%i==0): count=count+1 break i=i+1 if(count==0 and num !=1): print("%d is a prime number"%num) else: print("%d is not a prime number"%num)
When the above code is executed, it produces the following result
Case 1
Please enter a number: 23 23 is a prime number
Case 2
Please enter a number: 34 34 is not a prime number
Code to check whether the number is prime or not – using function
Program 3
#check whether the given number is prime or not def is_Prime(num): count=0 for i in range(2,num//2+1): if(num%i==0): count=count+1 return count num=int(input("Please enter a number for prime check: ")) result=is_Prime(num) if(result==0 and num!=1): print("%d is a prime number"%num) else: print("%d is not a prime number"%num)
When the above code is executed, it produces the following result
Case 1
Please enter a number for prime check: 79 79 is a prime number
Case 2
Please enter a number for prime check: 44 44 is not a prime number
Code to check whether the number is prime or not – using recursion
Program 4
#check whether the given number is prime or not using recursion def is_Prime(num,i=2):#function to recursion if(num<=2): return True if(num==2) else False if(num % i==0): return False if(i*i>num): return True return is_Prime(num,i+1) num=int(input("Please enter a number for prime check: ")) if(is_Prime(num)): print("%d is a prime number"%num) else: print("%d is not a prime number"%num)
When the above code is executed, it produces the following result
Case 1
Please enter a number for prime check: 109 109 is a prime number
Case 2
Please enter a number for prime check: 110 110 is not a prime number
Suggested for you
Similar post
Java programming code to check prime or not
C programming code to check prime or not