Learn about Python Program to Check Prime Number in the below code example. Also, refer to the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Python Program to Check Prime Number
A number is said to be a prime number if it is a positive integer greater than one that can be divided by one and itself.
Example: 2,3,7,11,13,…. etc
The below program checks whether the given number is prime or not.
Program:
# Python program to check if a number is prime or not
# input number
num = int(input('Enter a number: '))
# If number is greater than 1
if num > 1:
for i in range(2, num//2):
if (num % i) == 0:
print(num, "is not a prime number")
break
else:
print(num, "is a prime number")
else:
print(num, "is not a prime number")
Output:
Enter a number: 11
11 is a prime number
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Code : Convert Hex String to ASCII String in Python