write a code using a function to check whether a given number is prime number or not in python

Prime numbers hold a significant place in mathematics and computer science. They are the building blocks for many algorithms and play a crucial role in various fields. 

In this blog post, we will explore how to write a Python program to determine whether a given number is prime or not. To accomplish this, we will employ a function-based approach. 

We will discuss the concept of prime numbers, break down the code into three parts, and showcase the final code with two sample outputs. So, let's dive in and uncover the secrets of prime numbers in Python!

 

Title : write a code using a function to check whether a given number is prime number or not in python 

  • Understanding Prime Numbers

    Before we proceed with the code, let's have a brief overview of prime numbers. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In other words, it is only divisible by 1 and itself without any remainder. For example, 2, 3, 5, 7, 11, and 13 are prime numbers.
 
 
write a code using a function to check whether a given number is prime number or not in python

 

 

Designing the Prime Checking Function 

To determine whether a number is prime, we will create a function named check_prime(). This function will take an integer as input and return a boolean value, True if the number is prime, and False otherwise. Let's break down the code for the check_prime() function.



def check_prime(number):
if number <= 1:
return False

for i in range(2, int(number ** 0.5) + 1):
if number % i == 0:
return False

return True
 

In the code snippet above, we first check if the number is less than or equal to 1. Since prime numbers are defined as natural numbers greater than 1, any number less than or equal to 1 is not prime, and we return False immediately.

Next, we set up a loop that iterates from 2 to the square root of the number (inclusive). We utilize the int(number ** 0.5) expression to find the integer square root of the number. Inside the loop, we check if the number is divisible by any integer within the specified range. If it is divisible, we return False because it violates the prime number definition.

Finally, if the loop completes without finding any divisors, we conclude that the number is prime and return True.

 

 

Accepting User Input and Invoking the Function 

Now that we have the check_prime() function, we can proceed to accept user input and invoke the function to check whether the given number is prime or not. We will use the input() function to prompt the user for a number and convert it to an integer.


user_number = int(input("Enter a number: "))

if check_prime(user_number):
print(f"{user_number} is a prime number.")
else:
print(f"{user_number} is not a prime number.")


 

 

Complete Python Program



def check_prime(number):
if number <= 1:
return False

for i in range(2, int(number ** 0.5) + 1):
if number % i == 0:
return False

return True


user_number = int(input("Enter a number: "))

if check_prime(user_number):
print(f"{user_number} is a prime number.")
else:
print(f"{user_number} is not a prime number.")

 

 

Output 1:

Enter a number: 17
17 is a prime number.

 

Output 2:

Enter a number: 10
10 is not a prime number.

 

Conclusion:

In this blog post, we have explored the process of writing a Python program to check whether a given number is prime or not using a function-based approach. 
 
We discussed the concept of prime numbers and their significance. Then, we designed the check_prime() function to determine the number is prime or not.
 
Finally, we accepted user input, invoked the function, and displayed the result based on the returned boolean value. 
 
By employing this function in your Python programs, you can easily identify prime numbers and leverage their properties for various computations. 



Post a Comment

0 Comments