Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 76 additions & 2 deletions math/Simple-Calculator/Simple-Calculator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import math

from matplotlib.pylab import angle

class Calculator:
def __init__(self):
pass
Expand All @@ -13,11 +17,17 @@ def start_calculation(self):
print('7. Power')
print('8. Factorial')
print('9. Square root')
print('10. Exit')
print('10. Sine')
print('11. Cosine')
print('12. Tangent')
print('13. Cotangent')
print('14. Secant')
print('15. Cosecant')
print('16. Exit')

while True:
try:
user_input = int(input('Enter your choice to calculate(1-10): '))
user_input = int(input('Enter your choice to calculate(1-16): '))
if user_input == 1:
self.addition()
elif user_input == 2:
Expand All @@ -37,6 +47,18 @@ def start_calculation(self):
elif user_input == 9:
self.sqrt()
elif user_input == 10:
self.sine()
elif user_input == 11:
self.cosine()
elif user_input == 12:
self.tangent()
elif user_input == 13:
self.cotangent()
elif user_input == 14:
self.secant()
elif user_input == 15:
self.cosecant()
elif user_input == 16:
print('Goodbye! Have a good day.')
break
else:
Expand All @@ -49,6 +71,12 @@ def start_calculation(self):
except Exception as e:
print(f'Something went wrong: {e}')

def clean(self, x, eps=1e-10):
if abs(x) < eps:
return 0.0
return x


def addition(self):
a = int(input('enter first value: '))
b = int(input('enter second value: '))
Expand Down Expand Up @@ -103,7 +131,53 @@ def sqrt(self):
sqrt = num ** 0.5
print(f"Square root of {num} : {sqrt}")

def sine(self):
angle = float(input("Enter the angle in degrees: "))
rads = math.radians(angle)
sin_val = self.clean(math.sin(rads))
print(f"Sine of {angle} degrees is: {sin_val}")

def cosine(self):
angle = float(input("Enter the angle in degrees: "))
rads = math.radians(angle)
cos_val = self.clean(math.cos(rads))
print(f"Cosine of {angle} degrees is: {cos_val}")

def tangent(self):
angle = float(input("Enter the angle in degrees: "))
rads = math.radians(angle)
tan_val = self.clean(math.tan(rads))
print(f"Tangent of {angle} degrees is: {tan_val}")

def cotangent(self):
angle = float(input("Enter the angle in degrees: "))
rads = math.radians(angle)
tan_val = self.clean(math.tan(rads))
if tan_val != 0:
cot_val = 1 / tan_val
print(f"Cotangent of {angle} degrees is: {cot_val}")
else:
print(f"Cotangent of {angle} degrees is undefined.")

def secant(self):
angle = float(input("Enter the angle in degrees: "))
rads = math.radians(angle)
cos_val = self.clean(math.cos(rads))
if cos_val != 0:
sec_val = 1 / cos_val
print(f"Secant of {angle} degrees is: {sec_val}")
else:
print(f"Secant of {angle} degrees is undefined.")

def cosecant(self):
angle = float(input("Enter the angle in degrees: "))
rads = math.radians(angle)
sin_val = self.clean(math.sin(rads))
if sin_val != 0:
csc_val = 1 / sin_val
print(f"Cosecant of {angle} degrees is: {csc_val}")
else:
print(f"Cosecant of {angle} degrees is undefined.")

cal = Calculator()
cal.start_calculation()
Loading