Skip to content

Commit f4f60d6

Browse files
committed
add factorial, power and other functions and implemented OOP
1 parent fa36d25 commit f4f60d6

1 file changed

Lines changed: 103 additions & 80 deletions

File tree

Lines changed: 103 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,109 @@
1-
print("🧮 Simple Calculator 🧮")
2-
print("Perform basic arithmetic operations\n")
1+
class Calculator:
2+
def __init__(self):
3+
pass
34

4-
history = []
5+
def start_calculation(self):
6+
print("--------Menu---------")
7+
print('1. Addition')
8+
print('2. Subtraction')
9+
print('3. Multiplication')
10+
print('4. Division')
11+
print('5. Modulus')
12+
print('6. Floor Division')
13+
print('7. Power')
14+
print('8. Factorial')
15+
print('9. Square root')
16+
print('10. Exit')
517

6-
while True:
7-
print("=" * 40)
8-
print("📋 Choose an operation:")
9-
print("1️⃣ Addition (+)")
10-
print("2️⃣ Subtraction (-)")
11-
print("3️⃣ Multiplication (×)")
12-
print("4️⃣ Division (÷)")
13-
print("5️⃣ Modulus (%)")
14-
print("6️⃣ Power (^)")
15-
print("7️⃣ Show History")
16-
print("8️⃣ Exit")
17-
print("=" * 40)
18-
19-
choice = input("\n🎯 Enter your choice (1-8): ")
18+
while True:
19+
try:
20+
user_input = int(input('Enter your choice to calculate(1-10): '))
21+
if user_input == 1:
22+
self.addition()
23+
elif user_input == 2:
24+
self.substraction()
25+
elif user_input == 3:
26+
self.multiplication()
27+
elif user_input == 4:
28+
self.division()
29+
elif user_input == 5:
30+
self.modulus()
31+
elif user_input == 6:
32+
self.floor_division()
33+
elif user_input == 7:
34+
self.power()
35+
elif user_input == 8:
36+
self.factorial()
37+
elif user_input == 9:
38+
self.sqrt()
39+
elif user_input == 10:
40+
print('Goodbye! Have a good day.')
41+
break
42+
else:
43+
print('Wrong Choice.')
44+
45+
except ValueError:
46+
print('please enter correct option.')
47+
except ZeroDivisionError:
48+
print('Zero cannot be divided by zero.')
49+
except Exception as e:
50+
print(f'Something went wrong: {e}')
51+
52+
def addition(self):
53+
a = int(input('enter first value: '))
54+
b = int(input('enter second value: '))
55+
add = a + b
56+
print(f'The Result is: {add}')
2057

21-
if choice == '8':
22-
print("\n👋 Thanks for using the calculator! Goodbye!\n")
23-
break
58+
def substraction(self):
59+
a = int(input('enter first value: '))
60+
b = int(input('enter second value: '))
61+
minus = a - b
62+
print(f'The Result is: {minus}')
2463

25-
elif choice == '7':
26-
print("\n📜 Calculation History:\n")
27-
28-
if len(history) == 0:
29-
print("📭 No calculations found in history.\n")
30-
else:
31-
for item in history:
32-
print(f"➡️ {item}")
33-
print()
64+
def division(self):
65+
a = int(input('enter first value: '))
66+
b = int(input('enter second value: '))
67+
div = a / b
68+
print(f'The Result is: {div}')
3469

35-
elif choice in ['1', '2', '3', '4', '5', '6']:
36-
try:
37-
num1 = float(input("\n📥 Enter first number: "))
38-
num2 = float(input("📥 Enter second number: "))
39-
40-
if choice == '1':
41-
result = num1 + num2
42-
calculation = f"{num1} + {num2} = {result}"
43-
history.append(calculation)
44-
print(f"\n✨ Result: {calculation}\n")
45-
46-
elif choice == '2':
47-
result = num1 - num2
48-
calculation = f"{num1} - {num2} = {result}"
49-
history.append(calculation)
50-
print(f"\n✨ Result: {calculation}\n")
51-
52-
elif choice == '3':
53-
result = num1 * num2
54-
calculation = f"{num1} × {num2} = {result}"
55-
history.append(calculation)
56-
print(f"\n✨ Result: {calculation}\n")
57-
58-
elif choice == '4':
59-
if num2 == 0:
60-
print("\n❌ Error! Division by zero is not allowed.\n")
61-
else:
62-
result = num1 / num2
63-
calculation = f"{num1} ÷ {num2} = {result}"
64-
history.append(calculation)
65-
print(f"\n✨ Result: {calculation}\n")
66-
67-
elif choice == '5':
68-
if num2 == 0:
69-
print("\n❌ Error! Modulus by zero is not allowed.\n")
70-
else:
71-
result = num1 % num2
72-
calculation = f"{num1} % {num2} = {result}"
73-
history.append(calculation)
74-
print(f"\n✨ Result: {calculation}\n")
75-
76-
elif choice == '6':
77-
result = num1 ** num2
78-
calculation = f"{num1} ^ {num2} = {result}"
79-
history.append(calculation)
80-
print(f"\n✨ Result: {calculation}\n")
81-
82-
except ValueError:
83-
print("\n❌ Invalid input! Please enter valid numbers.\n")
70+
def multiplication(self):
71+
a = int(input('enter first value: '))
72+
b = int(input('enter second value: '))
73+
multiplication = a * b
74+
print(f'The Result is: {multiplication}')
75+
76+
def floor_division(self):
77+
a = int(input('enter first value: '))
78+
b = int(input('enter second value: '))
79+
floor = a // b
80+
print(f'The Result is: {floor}')
81+
82+
def modulus(self):
83+
a = int(input('enter first value: '))
84+
b = int(input('enter second value: '))
85+
mod = a % b
86+
print(f'The Result is: {mod}')
87+
88+
def power(self):
89+
a = int(input('enter first value: '))
90+
b = int(input('enter second value: '))
91+
power = a ** b
92+
print(f'The Result is: {power}')
93+
94+
def factorial(self):
95+
a = int(input("Enter the number: "))
96+
fact = 1
97+
for i in range(1,a+1):
98+
fact = fact * i
99+
print(f"Factorial is: {fact}")
84100

85-
else:
86-
print("\n❌ Invalid choice! Please select 1-8.\n")
101+
def sqrt(self):
102+
num = int(input("Enter the number: "))
103+
sqrt = num ** 0.5
104+
print(f"Square root of {num} : {sqrt}")
105+
106+
107+
108+
cal = Calculator()
109+
cal.start_calculation()

0 commit comments

Comments
 (0)