Python Program to Check Prime Number
Python Program to Check Prime Number | |
What is Prime Number?
A positive integer greater than 1 which has no other factorsexcept 1 and therefore the number itself is named a prime number.2, 3, 5, 7 etc. are prime numbers as they are doing not have theother factors. But 6 isn’t prime (it is composite) since, 2 x 3 = 6. |
|
CODE | OUTPUT |
# Python program to see if the input number is prime or not
num = 407 # take input from the user # num = int(input(“Enter a number: “)) # prime numbers are greater than 1 if num > 1: # check for factors for i in range(2,num): if (num % i) == 0: print(num,”is not a prime number”) print(i,”times”,num//i,”is”,num) break else: print(num,”is a prime number”) # if input number is less than # or adequate to 1, it’s not prime else: print (num,”is not a prime number”) |
407 is not a prime number
11 times 37 is 407
|
In this program, variable num is checked if it's prime or not. Numbers but or adequate to 1 aren't prime numbers. Hence, we only proceed if the num is bigger than 1. We check if num is strictly divisible by any number from 2 to num - 1. If we discover an element therein range, the amount isn't prime. Else the number is prime. We can decrease the range of numbers where we glance for factors. In the above program, our search range is from 2 to num - 1. We could have used the range, [2, num / 2] or [2, num ** 0.5]. The later range is predicated on the very fact that a number must have an element but root of that number; otherwise the amount is prime. You can change the worth of variable num within the above ASCII text file and test for other integers (if you want). |