Skip to content

Commit ab491b3

Browse files
Merge pull request steam-bell-92#313 from sangitabera/add_math_functions
add factorial, power and other functions and implemented OOP
2 parents fe54b27 + b35f2b8 commit ab491b3

1 file changed

Lines changed: 109 additions & 275 deletions

File tree

Lines changed: 109 additions & 275 deletions
Original file line numberDiff line numberDiff line change
@@ -1,275 +1,109 @@
1-
import math
2-
3-
print("🧮 Advanced Scientific Calculator 🧮")
4-
print("📚 Perform arithmetic and scientific operations\n")
5-
6-
history = []
7-
8-
while True:
9-
10-
print("=" * 60)
11-
print("📋 Choose an Operation:")
12-
print("1️⃣ Addition (+)")
13-
print("2️⃣ Subtraction (-)")
14-
print("3️⃣ Multiplication (×)")
15-
print("4️⃣ Division (÷)")
16-
print("5️⃣ Modulus (%)")
17-
print("6️⃣ Power (^)")
18-
print("7️⃣ Square Root (√)")
19-
print("8️⃣ Percentage")
20-
print("9️⃣ Decimal Calculation")
21-
print("🔟 Sine (sin)")
22-
print("1️⃣1️⃣ Cosine (cos)")
23-
print("1️⃣2️⃣ Tangent (tan)")
24-
print("1️⃣3️⃣ Logarithm (log)")
25-
print("1️⃣4️⃣ Factorial (!)")
26-
print("1️⃣5️⃣ Show History")
27-
print("1️⃣6️⃣ Clear History")
28-
print("1️⃣7️⃣ Delete Last History")
29-
print("0️⃣ Exit")
30-
print("=" * 60)
31-
32-
choice = input("\n🎯 Enter your choice: ")
33-
34-
# EXIT
35-
if choice == '0':
36-
print("\n👋 Thanks for using the calculator. Goodbye!\n")
37-
break
38-
39-
# SHOW HISTORY
40-
elif choice == '15':
41-
42-
print("\n📜 Calculation History:\n")
43-
44-
if len(history) == 0:
45-
print("📭 No calculations found.\n")
46-
47-
else:
48-
for item in history:
49-
print(f"➡️ {item}")
50-
51-
print()
52-
53-
# CLEAR HISTORY
54-
elif choice == '16':
55-
56-
history.clear()
57-
print("\n🗑️ History Cleared Successfully!\n")
58-
59-
# DELETE LAST HISTORY
60-
elif choice == '17':
61-
62-
if len(history) == 0:
63-
print("\n📭 No history to delete.\n")
64-
65-
else:
66-
deleted = history.pop()
67-
print(f"\n🗑️ Deleted: {deleted}\n")
68-
69-
# SQUARE ROOT
70-
elif choice == '7':
71-
72-
try:
73-
num = float(input("\n📥 Enter number: "))
74-
75-
if num < 0:
76-
print("\n❌ Cannot find square root of negative number!\n")
77-
continue
78-
79-
result = math.sqrt(num)
80-
81-
calculation = f"√{num} = {result}"
82-
83-
history.append(calculation)
84-
85-
print(f"\n✨ Result: {calculation}\n")
86-
87-
except ValueError:
88-
print("\n❌ Invalid input!\n")
89-
90-
# PERCENTAGE
91-
elif choice == '8':
92-
93-
try:
94-
num = float(input("\n📥 Enter number: "))
95-
96-
result = num / 100
97-
98-
calculation = f"{num}% = {result}"
99-
100-
history.append(calculation)
101-
102-
print(f"\n✨ Result: {calculation}\n")
103-
104-
except ValueError:
105-
print("\n❌ Invalid input!\n")
106-
107-
# DECIMAL CALCULATION
108-
elif choice == '9':
109-
110-
try:
111-
num1 = float(input("\n📥 Enter first decimal number: "))
112-
num2 = float(input("📥 Enter second decimal number: "))
113-
114-
result = num1 + num2
115-
116-
calculation = f"{num1} + {num2} = {result}"
117-
118-
history.append(calculation)
119-
120-
print(f"\n✨ Result: {calculation}\n")
121-
122-
except ValueError:
123-
print("\n❌ Invalid decimal input!\n")
124-
125-
# SINE
126-
elif choice == '10':
127-
128-
try:
129-
angle = float(input("\n📥 Enter angle in degrees: "))
130-
131-
result = math.sin(math.radians(angle))
132-
133-
calculation = f"sin({angle}) = {result}"
134-
135-
history.append(calculation)
136-
137-
print(f"\n✨ Result: {calculation}\n")
138-
139-
except ValueError:
140-
print("\n❌ Invalid input!\n")
141-
142-
# COSINE
143-
elif choice == '11':
144-
145-
try:
146-
angle = float(input("\n📥 Enter angle in degrees: "))
147-
148-
result = math.cos(math.radians(angle))
149-
150-
calculation = f"cos({angle}) = {result}"
151-
152-
history.append(calculation)
153-
154-
print(f"\n✨ Result: {calculation}\n")
155-
156-
except ValueError:
157-
print("\n❌ Invalid input!\n")
158-
159-
# TANGENT
160-
elif choice == '12':
161-
162-
try:
163-
angle = float(input("\n📥 Enter angle in degrees: "))
164-
165-
result = math.tan(math.radians(angle))
166-
167-
calculation = f"tan({angle}) = {result}"
168-
169-
history.append(calculation)
170-
171-
print(f"\n✨ Result: {calculation}\n")
172-
173-
except ValueError:
174-
print("\n❌ Invalid input!\n")
175-
176-
# LOGARITHM
177-
elif choice == '13':
178-
179-
try:
180-
num = float(input("\n📥 Enter number: "))
181-
182-
if num <= 0:
183-
print("\n❌ Logarithm only works for positive numbers!\n")
184-
continue
185-
186-
result = math.log10(num)
187-
188-
calculation = f"log({num}) = {result}"
189-
190-
history.append(calculation)
191-
192-
print(f"\n✨ Result: {calculation}\n")
193-
194-
except ValueError:
195-
print("\n❌ Invalid input!\n")
196-
197-
# FACTORIAL
198-
elif choice == '14':
199-
200-
try:
201-
num = int(input("\n📥 Enter positive integer: "))
202-
203-
if num < 0:
204-
print("\n❌ Factorial not defined for negative numbers!\n")
205-
continue
206-
207-
result = math.factorial(num)
208-
209-
calculation = f"{num}! = {result}"
210-
211-
history.append(calculation)
212-
213-
print(f"\n✨ Result: {calculation}\n")
214-
215-
except ValueError:
216-
print("\n❌ Invalid input!\n")
217-
218-
# BASIC OPERATIONS
219-
elif choice in ['1', '2', '3', '4', '5', '6']:
220-
221-
try:
222-
num1 = float(input("\n📥 Enter first number: "))
223-
num2 = float(input("📥 Enter second number: "))
224-
225-
if choice == '1':
226-
227-
result = num1 + num2
228-
calculation = f"{num1} + {num2} = {result}"
229-
230-
elif choice == '2':
231-
232-
result = num1 - num2
233-
calculation = f"{num1} - {num2} = {result}"
234-
235-
elif choice == '3':
236-
237-
result = num1 * num2
238-
calculation = f"{num1} × {num2} = {result}"
239-
240-
elif choice == '4':
241-
242-
if num2 == 0:
243-
print("\n❌ Division by zero is not allowed!\n")
244-
continue
245-
246-
result = num1 / num2
247-
calculation = f"{num1} ÷ {num2} = {result}"
248-
249-
# MODULUS
250-
elif choice == '5':
251-
252-
if num2 == 0:
253-
print("\n❌ Modulus by zero is not allowed!\n")
254-
continue
255-
256-
result = num1 % num2
257-
calculation = f"{num1} % {num2} = {result}"
258-
259-
# POWER
260-
elif choice == '6':
261-
262-
result = num1 ** num2
263-
calculation = f"{num1}^{num2} = {result}"
264-
265-
history.append(calculation)
266-
267-
print(f"\n✨ Result: {calculation}\n")
268-
269-
except ValueError:
270-
print("\n❌ Please enter valid numbers!\n")
271-
272-
# INVALID CHOICE
273-
else:
274-
275-
print("\n❌ Invalid choice! Please select a valid option.\n")
1+
class Calculator:
2+
def __init__(self):
3+
pass
4+
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')
17+
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}')
57+
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}')
63+
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}')
69+
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}")
100+
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)