In this post, you will learn a python program to check given number is prime or not but before writing a program we should know what is a prime number.
What is prime number?
A number that is only divisible by 1 and itself is known as a prime number. An example of a prime number is 2,3,5,7,11,13,17,19… etc.
we should write this program in three different ways:
- Simple way (By taking user input).
- by using function.
- Except error if user enter wrong input.
1. Simple way (By taking user input)
Write a python program to check given number is prime or not by taking user input.
Before writing this program you should know about:
Source Code
num = int(input("enter a number to check entered number is prime or no :"))
p = 0
print(f"{num} is divide by :",end=" ")
for i in range(2,num):
if num%i==0:
print(i, end=",")
p=1
print("\t")
if p==1 and num!=2:
print(f"{num} is not a prime number")
else:
print(f"{num} is a prime number")
Output-1
enter a number to check entered number is prime or no :4
4 is divide by : 2,
4 is not a prime number
Output-2
enter a number to check entered number is prime or no :2
2 is divide by :
2 is a prime number
Explanation
num
variable is used to take integer value as input from the user.
p
variable (which is initially 0
) is used to check for prime numbers when p=1
which means that num
is not a prime number.
with the help of for loop
and if statement
we check for num
is divisible by any other number (Except 1 or num
) or not, if a number is divisible by any other number then p=1
that shows num
is not a prime number.
2. prime number By using function
Write python program to check given number is prime or not using function.
Before writing this program you should know about:
Source Code
def prime_number(n):
check_prime = 0
for i in range(2,n):
if n%i == 0:
check_prime = 1
if check_prime == 1:
return f'{n} is not a prime number'
else:
return f'{n} is a prime number'
# you can also take user input instead of writing direct value
# user input - demo
# num1 = int(input("ENter a number: "))
# prime_number(num1)
print(prime_number(2))
print(prime_number(4))
print(prime_number(5))
print(prime_number(6))
print(prime_number(7))
print(prime_number(9))
Output
2 is a prime number
4 is not a prime number
5 is a prime number
6 is not a prime number
7 is a prime number
9 is not a prime number
3. Except error if user enter wrong input.
Write a python program to check given number is prime number or not and also handle exception if user input wrong data.
before writing this program you should know about:
Source code
def prime_number(n):
check_prime = 0
for i in range(2,n):
if n%i == 0:
check_prime = 1
if check_prime == 1:
return f'{n} is not a prime number'
else:
return f'{n} is a prime number'
while True:
try:
number = int(input("Enter a number: "))
if type(number) == int:
break
except ValueError:
print("please enter number only")
print(prime_number(number))
Output-1
Enter a number: seven
please enter number only
Enter a number: 7
7 is a prime number
Output-2
Enter a number: four
please enter number only
Enter a number: 4
4 is not a prime number
Hope this post help you and add good value to your life -thank you.
1 Comment
Thanks for your blog, nice to read. Do not stop.