Write a python program to check if a given number is prime number using function.
Answers
Answer:
hi...
Explanation:
def prime(n):
if n==2:
print("it's a prime ")
else:
for i in range(2,n):
if n%i==0:
print("it's not a prime")
else:
print("its a prime")
#main
a=int(input("enter the number"))
prime(a)
hope it helps you ☺️
From math import sqrt
num=int(input("enter the number"))
prime(num) #pass the number to the function
def prime(n):
flag = 0
if(n > 1):
for k in range(2, int(sqrt(n)) + 1):
if (n % k == 0):
flag = 1
break
if (flag == 0):
print(n," is a Prime Number!")
else:
print(n," is Not a Prime Number!")
else:
print(n," is Not a Prime Number!")
Code Explanation:-
It imports square root function from the maths file.
In the variable name num user input is taken.
Algorithm:
1. Create a for loop from 2 and end it at the integer value of the number's floor square root.
2. Verify that the number can be divided by two.
3. Continue until the number's square root has been determined.
4. If a number can be divided by any other number, it is not prime.
5.If not, it's a prime number.
To know more refer the links:-
https://brainly.in/question/14689905
https://brainly.in/question/2360394
#SPJ3