Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
C++ code to check whether the number is prime or not

Python code to check whether the number is prime or not

Posted on October 15, 2020October 15, 2020

Table of Contents

  • Python code to check whether the number is prime or not
    • Code to check whether the number is prime or not
      • Code to check whether the number is prime or not – using for loop
      • Code to check whether the number is prime or not – using while loop
      • Code to check whether the number is prime or not – using function
      • Code to check whether the number is prime or not – using recursion

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

Data type in Python

Operator in Python

if statement in Python

for statement in Python

function in Python language

 

Similar post

Java programming code to check prime or not

C programming code to check prime or not

C++ programming code to check prime or not

Python programming code to check prime or not

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes