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
- Create a function prime
- Use the def keyword to define the function.
- Pass a parameter i.e. number to the function.
- Create a count variable and assign its value equal to 0.
- Use for loop with the range function to check whether a number is a prime number or not.
- If a number divides the input number then add 1 to the count variable.
- If a number is divisible by 1 and by itself then it is a prime number.
- Print the output.
- 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
Python function program to find the even numbers from a given list.
Python function program to execute a string containing Python code.
Python function program to access a function inside a function.
Python function program to find the LCM of two numbers.
Python function program to calculate the sum of numbers from 0 to 10.