A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. Write a function named is_prime that takes an integer argument and returns True if the number is prime and False otherwise. (Assume that the argument is an integer greater than 1, i.e. no need to check for erroneous input.)

Respuesta :

Answer:

This is a function written in Python Programming Language to check whether a given number is prime or not.

def is_prime(n):

   if (n==1):

       return False

   elif (n==2):

       return True;

   else:

       for x in range(2,n):

           if(n % x==0):

               return False

       return True              

print(is_prime(9))

Explanation:

Following are the Python program to check prime number condition:

Program Explanation:

  • Defining a method "is_prime" that takes the "num" variable in the parameter.
  • Inside the method, a for loop is declared that check the prime number value, and return a boolean value.
  • Outside the method, "num" is declared that inputs value, and use a conditional block statement to check value and print its return value.  

Program:

def is_prime(num):#defining a method is_prime that takes one parameter

  for i in range(2, num):#defining a loop for check prime number condition

      if num % i == 0:#defining if block that check number is even

          return False#return value false

  return True#return value True

num = int(input("Input an integer greater than 1: "))#defining a num variable that input number

if is_prime(num):#defining if block that calls method value

   print("{:d} is a prime ".format(num))#print number value that is prime

else:#else block

   print("{:d} is not a prime ".format(num))#print number value that is not prime

Output:

Please find the attached file.

Learn more:

brainly.com/question/4982426

Ver imagen codiepienagoya
Ver imagen codiepienagoya