Q:

Python program to check prime number

belongs to collection: Python basic programs

0

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.

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

Example:

    Input:
    num = 59

    Output:
    59 is a prime number

    Input:
    num = 123

    Output:
    123 is not a prime number

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to check prime number in Python

# 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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to calculate prime numbers (using d... >>
<< Python | Program to calculate n-th term of a Fibon...