- 0
Notifications Mark All Read
No New notification
- Login
- Get Prime
Program to check if the given number is Prime or not in Python
Prime or not in Python
In this article we will see a program to find whether a number is Prime or not in Python. A number is said to be a prime number if and only if it is only divisible by 1 or itself (factors==>1,number itself) . If a number has more than two factors than the number is said to be not a prime number.- Sample input: 17
- Sample output: The given number is prime
Method 1
Algorithm:
- Step 1: Initialize the variable a and count to zero
- Step 2: Read the input
- Step 3: Set a to n//2 (// in python implies integer division) , we can make a to square root of number to make it more efficient
- Step 4: iterate through 2 to a+1 and check whether it is divisible by i or not
- Step 5 : if divisible , then print “not a prime number” and set count to 1 and break from the loop
else continue iteration - Step 6 : if count is still zero then the given number is prime number
Python code:
a = 0 count = 0 n=int(input("Enter the number to check if it is prime or not: ")) a = n // 2; for i in range(2,a+1): if (n % i == 0): print("The given number is not prime") count = 1 break if (count == 0): print("The given number is prime")
Enter the number to check if it is prime or not: 17
The given number is prime
Method 2
Algorithm
- Step 1: Take an empty array
- Step 2: Read the number
- Step 3: Iterate a from 1 to the number
- Step 4: If number is divisible by iterator i then append the number to factors array
- Step 5: if length of factors array is 2 then given number is prime
else the given number is not prime number
(for more clarity factors of numbers also printed)
Python Code:
factors=[ ] n=int(input(“Enter the number to check if it is prime or not: “)) for i in range(1,n+1): if (n % i == 0): factors.append(i) if (len(factors)==2): print(“The given number is prime”) else: print(“The given number is not prime number”) print(“Factors of given number is”,factors)
Enter the number to check if it is prime or not: 16
The given number is not prime number
[1, 2, 4, 8, 16]
- Positive or Negative number: C | C++ | Java
- Even or Odd number: C | C++ | Java
- Sum of First N Natural numbers: C | C++ | Java
- Sum of N natural numbers: C | C++ | Java
- Sum of numbers in a given range: C | C++ | Java
- Greatest of two numbers: C | C++ | Java
- Greatest of the Three numbers: C | C++ | Java
- Leap year or not: C | C++ | Java
- Prime number: C | C++ | Java
- Prime number within a given range: C | C++ | Java
- Factorial of a number: C | C++ | Java
- Sum of digits of a number: C | C++ | Java
- Reverse of a number : C | C++ | Java
- Palindrome number: C | C++ | Java
- Armstrong number : C | C++ | Java
- Armstrong number in a given range : C | C++ | Java
- Fibonacci Series upto nth term : C | C++ | Java
- Factorial of a number : C | C++ | Java
- Power of a number : C | C++ | Java
- Factor of a number : C | C++ | Java
- Strong number : C | C++ | Java
- Perfect number : C | C++ | Java
- Automorphic number : C | C++ | Java
- Harshad number : C | C++ | Java
- Abundant number : C| C++ | Java
- Friendly pair : C | C++ | Java
Login/Signup to comment
num=int(input(“enter a number:”))
if num>1:
for i in range(2,num):
if(num%i)==0:
print(num,”is not prime number”)
else:
print(num,”is prime number”)
def f(x):
if x>3:
if ((x**2)-(1))%24==0:
print(“yes it is prime no”)
else:
print(“no its not”)
else:
print(“invalid value”)
f(X)
#ENTER THE ANY VALUE AT X TO GET ANSWER
#CODE BY ABHISHEK PATIL
#FOR PYTHON USER
# Easy to Understand
n = int(input(‘enter a number:’))
a = n//2
for I in range(2, a+1 ):
if n% I == 0:
print(‘given number is not a prime number’)
break
else:
print(‘given number is a prime number’)
x = int(input(‘Enter a year:’))
for i in range(2,x):
if x%i == 0:
print(‘Not Prime’)
break
else:
print(‘Prime’)
num = int(input(“enter no. to check : “))
for i in range(2,num):
if num % i== 0:
print(‘not a prime mo.’)
break
else:
print(“prime no.”)
n = int(input(“Enter the number: “))
c=0
for i in range(1,10):
if n%i==0:
c+=1
if(c>2):
print(“Not prime”)
else:
print(“prime”)
N=int(input(“Enter the Number for check !”))
if N!=1:
for i in range(2,N):
if N%i==0:
print(f”{N} is Not a prime Number”)
break
else:
print(f”{N} is a prime Number”)
else:
print(f”{N} is always prime number”)
num=int(input(“Enter a number: “))
i=2
count=0
while i=1:
print (“{} is not a prime number”.format(num))
else:
print (“{} is a prime number”.format(num))