forked from fenyx-it-academy/Class4-PythonModule-Week4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathW4_Q2_LCM.py
More file actions
40 lines (33 loc) · 1.29 KB
/
W4_Q2_LCM.py
File metadata and controls
40 lines (33 loc) · 1.29 KB
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
#C@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@1
# PyCoder Coding Class:4 @
# Cabir Erguven @
# Week : 4 @
# Question : 2 @
# Date: 30.01.2021 @
#C@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@1
#
# Calculate the least common multiple (L.C.M.) of four numbers.
# So that I can find the least common multiple (L.C.M.) of my inputs.
#
# Acceptance Criteria:
#
# Ask user to enter the four numbers.
# Use try/except blocks to verify input entries and warn the user for Nan or non numerical inputs.
# Calculate the least common multiple (L.C.M.) of four numbers
# Use gcd function in module of math
import math
list = []
res = 1
a = 0
for x in range(4):
while True:
try:
a = int(input("Enter four numbers to calculate their LCM: "))
break
except ValueError:
print("You entered NaN, please enter the numbers correctly")
list.append(a)
lcf_step1 = int((list[0] * list[1]) / math.gcd(list[0], list[1])) # recursive can be used
lcf_step2 = int((lcf_step1 * list[2]) / math.gcd(lcf_step1, list[2]))
lcf_step3 = int((lcf_step2 * list[3]) / math.gcd(lcf_step2, list[3]))
print("LCF of", list[0], list[1], list[2], list[3], "is:", lcf_step3)