0% found this document useful (0 votes)
7 views2 pages

Python Basic Calculator Functions

Uploaded by

granthikbudhwar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Python Basic Calculator Functions

Uploaded by

granthikbudhwar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import math

def add(x, y):


return x + y

def subtract(x, y):


return x - y

def multiply(x, y):


return x * y

def divide(x, y):


if y == 0:
return "Error: Division by zero"
return x / y

def power(x, y):


return x ** y

def sqrt(x):
if x < 0:
return "Error: Negative square root"
return [Link](x)

def sin(x):
return [Link]([Link](x))

def cos(x):
return [Link]([Link](x))

def tan(x):
return [Link]([Link](x))

def log(x, base=10):


if x <= 0:
return "Error: Logarithm undefined"
return [Link](x, base)

def factorial(x):
if x < 0:
return "Error: Factorial of negative number"
return [Link](x)

def calculator():
while True:
print("\nOptions:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Power")
print("6. Square Root")
print("7. Sine")
print("8. Cosine")
print("9. Tangent")
print("10. Logarithm")
print("11. Factorial")
print("12. Exit")
choice = input("Choose an operation (1-12): ")

if choice == '12':
break

if choice in ('1', '2', '3', '4', '5'):


num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print("Result:", add(num1, num2))
elif choice == '2':
print("Result:", subtract(num1, num2))
elif choice == '3':
print("Result:", multiply(num1, num2))
elif choice == '4':
print("Result:", divide(num1, num2))
elif choice == '5':
print("Result:", power(num1, num2))
elif choice == '6':
num = float(input("Enter number: "))
print("Result:", sqrt(num))
elif choice in ('7', '8', '9'):
angle = float(input("Enter angle in degrees: "))
if choice == '7':
print("Result:", sin(angle))
elif choice == '8':
print("Result:", cos(angle))
elif choice == '9':
print("Result:", tan(angle))
elif choice == '10':
num = float(input("Enter number: "))
base = float(input("Enter base (default is 10): ") or 10)
print("Result:", log(num, base))
elif choice == '11':
num = int(input("Enter number: "))
print("Result:", factorial(num))
else:
print("Invalid input, try again.")

calculator()

You might also like