Python program to check prime number

Python | Prime Number Check Program: Here, we will implement a Python program to check whether a given number is a prime number or not? By IncludeHelp Last updated : April 14, 2023

What is a prime number?

A prime number is a natural number that is greater than 1 and cannot be formed by multiplying two smaller natural numbers.

Python Prime Number Check Program

Given a number num, we have to check whether num is a prime number or not.

Sample Input/Output

Input:
num = 59

Output:
59 is a prime number

Input:
num = 123

Output:
123 is not a prime number

Python program to check prime number

# Python program to check prime number

# Function to check prime number 
def isPrime(n): 
    return all([(n % j) for j in range(2, int(n/2)+1)]) and n>1
  
# Main code
num = 59
if isPrime(num):
  print(num, "is a prime number")
else:
  print(num, "is not a prime number")  

num = 7
if isPrime(num):
  print(num, "is a prime number")
else:
  print(num, "is not a prime number")  

num = 123
if isPrime(num):
  print(num, "is a prime number")
else:
  print(num, "is not a prime number") 

Output

59 is a prime number
7 is a prime number
123 is not a prime number

Python Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.