Ghmustafa11's picture
Create app.py
2039b8f verified
import streamlit as st
import math
# Title of the app
st.title("Scientific Calculator Application")
st.subheader("Perform advanced mathematical operations.")
# Operation selection
operation = st.selectbox(
"Select an operation:",
[
"Addition", "Subtraction", "Multiplication", "Division",
"Square Root", "Exponentiation", "Sine", "Cosine", "Tangent",
"Logarithm (Base 10)", "Logarithm (Natural)", "Factorial"
]
)
# Input fields for numbers
num1 = st.text_input("Enter the first number:")
if operation not in ["Sine", "Cosine", "Tangent", "Logarithm (Base 10)", "Logarithm (Natural)", "Factorial"]:
num2 = st.text_input("Enter the second number:")
# Button to trigger calculation
if st.button("Calculate"):
try:
# Handle different operations
if operation in ["Addition", "Subtraction", "Multiplication", "Division"]:
num1 = float(num1)
num2 = float(num2)
if operation == "Addition":
result = num1 + num2
st.success(f"The result of {num1} + {num2} is: {result}")
elif operation == "Subtraction":
result = num1 - num2
st.success(f"The result of {num1} - {num2} is: {result}")
elif operation == "Multiplication":
result = num1 * num2
st.success(f"The result of {num1} * {num2} is: {result}")
elif operation == "Division":
if num2 != 0:
result = num1 / num2
st.success(f"The result of {num1} / {num2} is: {result}")
else:
st.error("Error: Division by zero is not allowed!")
elif operation == "Square Root":
num1 = float(num1)
if num1 >= 0:
result = math.sqrt(num1)
st.success(f"The square root of {num1} is: {result}")
else:
st.error("Error: Square root of a negative number is not allowed!")
elif operation == "Exponentiation":
num1 = float(num1)
num2 = float(num2)
result = math.pow(num1, num2)
st.success(f"The result of {num1} ^ {num2} is: {result}")
elif operation == "Sine":
num1 = float(num1)
result = math.sin(math.radians(num1))
st.success(f"The sine of {num1}° is: {result}")
elif operation == "Cosine":
num1 = float(num1)
result = math.cos(math.radians(num1))
st.success(f"The cosine of {num1}° is: {result}")
elif operation == "Tangent":
num1 = float(num1)
result = math.tan(math.radians(num1))
st.success(f"The tangent of {num1}° is: {result}")
elif operation == "Logarithm (Base 10)":
num1 = float(num1)
if num1 > 0:
result = math.log10(num1)
st.success(f"The log10 of {num1} is: {result}")
else:
st.error("Error: Logarithm of non-positive numbers is undefined.")
elif operation == "Logarithm (Natural)":
num1 = float(num1)
if num1 > 0:
result = math.log(num1)
st.success(f"The natural logarithm of {num1} is: {result}")
else:
st.error("Error: Logarithm of non-positive numbers is undefined.")
elif operation == "Factorial":
num1 = int(num1)
if num1 >= 0:
result = math.factorial(num1)
st.success(f"The factorial of {num1} is: {result}")
else:
st.error("Error: Factorial is only defined for non-negative integers.")
except ValueError:
st.error("Invalid input! Please enter numeric values.")