Python program to check prime number

Write a python program to check whether the given number is prime or not. Let’s create some examples in python to validate a prime number, also, will find the total count of prime numbers from an array.

A number that is divisible by 1 or itself is a called prime number like 7, 17, 19, 23, etc. A prime number can not be a negative number(-2,-5, etc..)

Check whether the number is prime or not in Python

Step 1 Create a function with a parameter

Step 2 Checking whether the number is negative or positive

Step 3 If it is positive then we run a loop from 2 to the range of number  and then check if the range number and the given number are divisible by it and give zero as a reminder if it is so then the number is not prime

Step 4 But if it is not divisible then the  number is a prime number

Step 5 As we are using the function we will use the return method to  give the output

Input and Output Sample

Input = 17

Output = it’s a prime number

Input = 16

Output = it’s a not prime number

Algorithm to find Prime number in Python

  • Creating a function with a parameter to take input
  • Check whether the entered parameter(input) is negative or positive
  • We will check whether the number is prime or not by running a loop with itself and dividing it and checking the remainder
  • If the remainder is zero it is not a prime number
  • If the remainder is not zero it is a prime number

Python Code to check whether the number is prime or not

def is_prime_number(num):
    if num > 1:
        for i in range(2, int(num/2)+1):
            if (num % i) == 0:
                return "is not a prime number"

        else:
             return "is a prime number"
    else:
        return "is not a prime number"

num=17
print(num,is_prime_number(num))
num2=16
print(num2,is_prime_number(num2))

Output

 

In this article part, we have seen how to check whether the number is a prime number or not, if we want the value as true or false we just replace the return part with true and false as per the condition.

Write a python program to find the count of prime numbers in an array

Step 1 we import our previous file using the import keyword and give it an alias name for convenience

Step 2  Then creates a function that takes an array as a parameter

Step 3 Then set the counter at zero

Step 4 Using for loop and accessing a single element at a time to check whether the number is prime or not

Step 5 By the is_prime_number  method of the previous code, we give a single element and check whether the condition is satisfied or not

Step 6 If satisfied then the count stays the same while if not then the count will update once

Step 7 As we are using the function we return the result using the return keyword

Step 8 At the end, we create an instance to check our code

Algorithm to count prime numbers in an array

  • Importing the previous code of checking the prime number validation
  • Creating a function with a parameter that will take an array
  • Set a counter to zero to start counting the prime number
  • Now we run the array in the loop for accessing each element
  • Check whether the accessed element  is negative or positive
  • Then giving value to the function of the previous code, we will check whether the number is prime or not
  • Update count every time if it is a prime number

 Python code to count prime numbers in an array

import prime_number as pn
def total_prime_number(arr):
    count=0
    for i in arr:
        if pn.is_prime_number(i) == "is not a prime number":
            count=count
        else:
            count=count+1
    return count
arr=[24,31,27,23,54,45]x
print(total_prime_number(arr))

Output

In this article, we have learned how to check any prime number and in another part, we have also seen how to count all the prime numbers in a given array