-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu_driven1.py
More file actions
61 lines (47 loc) · 1.66 KB
/
menu_driven1.py
File metadata and controls
61 lines (47 loc) · 1.66 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def getfactor(x):
factors = [i for i in range(1, x + 1) if x % i == 0]
return factors
def isprime(x):
if x <= 1:
return False
for i in range(2, int(x ** 0.5) + 1):
if x % i == 0:
return False
return True
def iscomposite(x):
return not isprime(x) and x > 1
def isperfect(x):
return sum(i for i in range(1, x) if x % i == 0) == x
def isabundance(x):
return sum(i for i in range(1, x) if x % i == 0) > x
def menu():
while True:
print("\nMenu:")
print("1. Get factors of a number")
print("2. Check if a number is prime")
print("3. Check if a number is composite")
print("4. Check if a number is perfect")
print("5. Check if a number is abundant")
print("6. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
x = int(input("Enter a number: "))
print(f"Factors of {x}: {getfactor(x)}")
elif choice == 2:
x = int(input("Enter a number: "))
print(f"{x} is prime: {isprime(x)}")
elif choice == 3:
x = int(input("Enter a number: "))
print(f"{x} is composite: {iscomposite(x)}")
elif choice == 4:
x = int(input("Enter a number: "))
print(f"{x} is perfect: {isperfect(x)}")
elif choice == 5:
x = int(input("Enter a number: "))
print(f"{x} is abundant: {isabundance(x)}")
elif choice == 6:
print("Exiting the program...")
break
else:
print("Invalid choice. Please try again.")
menu()