-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_prime_number.py
47 lines (40 loc) · 1.07 KB
/
check_prime_number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- coding: utf-8 -*-
"""
Created on Wed May 30 15:46:15 2018
@author: Vishwesh Ravi Shrimali
"""
from math import sqrt
def checkInput(num):
if num.isdigit():
if int(num) <= 1:
print("Number should be more than 1.")
return "error"
return int(num)
else:
print("Invalid input (Enter a positive integer)")
return "error"
def getInput():
print("Welcome to Prime Checker")
num = input("Enter a positive integer: ").strip()
while checkInput(num)== "error":
num = input("Enter a positive integer: ")
num = checkInput(num)
if checkPrime(num):
print("%d is a prime number"%num)
else:
print("%d is NOT a prime number"%num)
def checkPrime(num):
if num==2:
return True
elif num%2==0:
return False
else:
for i in range(2,int(sqrt(num))+1):
if num%i == 0:
return False
return True
def main():
getInput()
print("Thank you")
if __name__ == "__main__":
main()