Program that take a number as a parameter and checks whether the number is prime or not

In this python function program, we will take a number as a parameter and checks whether the number is prime or not.

Prime Number: A prime number is a whole number greater than 1 that cannot be exactly divided by any whole number other than itself and 1.

What is Function?
It is a block of code that executes when it is called.
To create a function use def keyword.

Steps to solve the program
  1. Create a function prime
  2. Use the def keyword to define the function.
  3. Pass a parameter i.e. number to the function.
  4. Create a count variable and assign its value equal to 0.
  5. Use for loop with the range function to check whether a number is a prime number or not.
  6. If a number divides the input number then add 1 to the count variable.
  7. If a number is divisible by 1 and by itself then it is a prime number.
  8. Print the output.
  9. Take a number as input through the user and pass it to the function while calling the function.

				
					def prime(num):
    count = 0

    for i in range(2,num):
        if num%i == 0:
            count += 1

    if count > 0:
        print("It is not a prime number")
    else:
        print("It is a prime number")
        
num1 =  int(input("Enter a number: "))
prime(num1)
				
			

Output :

				
					Enter a number: 7
It is a prime number
				
			

Related Articles

program that takes a list and returns a new list with unique elements of the first list.

find the even numbers from a given list.

Leave a Comment