This article let's try to find if the given number is a prime or not Prime. A prime number is a number which has only two factor that is one and the number itself and none of other number can divide the number
Let's try to solve this using a Python program.
Write A Python Program To Check The Given Number Is Prime Number Or Not
Let's take a number as an integer variable and ask a user to enter a number using the input function call. Convert the user entered number into an integer type using int() type conversion.
Then try to find the square root of the given number and store this in a variable sqrt. We will be iterating up to the square root of the number and try dividing number from 2 - sqrt(number) range
number = int(input("Enter a Number : "))
sqrt = int(number ** 0.5) + 1
Once we get the square root of the given number, use the follow to iterate from 2 to the sqrt. get the index value and try dividing the number with the index if it is divisible then print number is not a prime number.
Once the for loop is completed using the for loop else part print the number is prime.
for index in range(2, sqrt):
if number % index == 0:
print("Number is Not Prime")
break
else:
print("It's a Prime Number")
Code
==================
number = int(input("Enter a Number : "))
sqrt = int(number ** 0.5) + 1
for index in range(2, sqrt):
if number % index == 0:
print("Number is Not Prime")
break
else:
print("It's a Prime Number")
OUTPUT
=============
Enter a Number : 5
It’s a Prime Number
Output
================
Enter a Number : 23
It's a Prime Number
Output
===============
Enter a Number : 222
Number is Not Prime
Conclusion
==============
Execute the above program by providing a random value as an input to the program and note down the result.
Comment it down below if you have any suggestions regarding the above problem.
0 Comments