Python Program - Check Prime Number
Here's a Python program to check if a given number is a prime number or not:
refer ot:theitroad.com# function to check if a number is prime def is_prime(n): # prime numbers are greater than 1 if n <= 1: return False # check for factors from 2 to n-1 for i in range(2, n): if n % i == 0: return False # if no factors found, the number is prime return True # get user input num = int(input("Enter a number: ")) # check if the number is prime if is_prime(num): print(num, "is a prime number") else: print(num, "is not a prime number")
The program first defines a function is_prime
that takes an integer n
as input and returns True
if the number is prime and False
otherwise. The function checks if the number is greater than 1, and then checks for factors from 2 to n-1. If any factors are found, the number is not prime and the function returns False
. If no factors are found, the number is prime and the function returns True
.
The program then prompts the user to enter a number, and checks if the number is prime using the is_prime
function. The program prints whether the number is prime or not.