ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Python program to check prime number

Python | Check prime or not: Here, we are going to learn how to check whether a given number is a prime number or not in Python programming language?
Submitted by IncludeHelp, on April 27, 2020

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

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

Python Basic Programs »

ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.