-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLCM_HCF.py
86 lines (69 loc) · 2.28 KB
/
LCM_HCF.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# importing modules
import math
def HCF(Num_1, Num_2): # HCF
return math.gcd(Num_1, Num_2)
def LCM(Num_1, Num_2): # LCM
return math.lcm(Num_1, Num_2)
def main():
while True:
print("LCM/HCF Finder")
while True:
L_or_H = input("Enter L for LCM and H for HCF: ").upper()
if L_or_H in ['H', 'L']:
break
# HCF
if L_or_H == 'H':
print("HCF Finder")
while True:
Num_1 = input("Enter first the number: ")
try:
Num_1 = int(Num_1)
break
except:
print("Invalid input please try again!")
while True:
Num_2 = input("Enter second the number: ")
try:
Num_2 = int(Num_2)
break
except:
print("Invalid input please try again!")
HCF(Num_1, Num_2)
print(f"HCF of {Num_1} and {Num_2} is {math.gcd(Num_1,Num_2)}.")
# LCM
else:
print("LCM Finder")
while True:
Num_1 = input("Enter first the number: ")
try:
Num_1 = int(Num_1)
break
except:
print("Invalid input please try again!")
while True:
Num_2 = input("Enter second the number: ")
try:
Num_2 = int(Num_2)
break
except:
print("Invalid input please try again!")
LCM(Num_1, Num_2)
print(f"LCM of {Num_1} and {Num_2} is {math.lcm(Num_1,Num_2)}.")
# Program looping
while True:
try:
Again = input(
"Do you want to run this feature again(y/n)? ").lower()
if Again in ['y', 'n']:
break
else:
raise Exception("Invalid input")
# ⬆ To raise an error so that except gets triggered
except Exception:
print("Invalid input please try again!")
if Again == 'y':
continue
else:
break
if __name__ == '__main__':
main()