AmnaHassan's picture
Update app.py
0f8360f verified
import streamlit as st
import numpy as np
st.set_page_config(page_title="Advanced Calculator", page_icon="🧮")
st.title("🧮 Advanced Calculator")
# Layout
col1, col2 = st.columns(2)
with col1:
num1 = st.number_input("Enter first number", format="%.5f", step=1.0)
with col2:
num2 = st.number_input("Enter second number (if needed)", format="%.5f", step=1.0)
# Operation selection
operation = st.selectbox(
"Choose an operation",
[
"Add", "Subtract", "Multiply", "Divide",
"Power", "Modulo", "Square Root", "Logarithm",
"Sine", "Cosine", "Tangent"
]
)
# Perform calculation
result = None
if st.button("Calculate"):
try:
if operation == "Add":
result = num1 + num2
elif operation == "Subtract":
result = num1 - num2
elif operation == "Multiply":
result = num1 * num2
elif operation == "Divide":
if num2 != 0:
result = num1 / num2
else:
st.error("Cannot divide by zero.")
elif operation == "Power":
result = np.power(num1, num2)
elif operation == "Modulo":
result = num1 % num2
elif operation == "Square Root":
if num1 >= 0:
result = np.sqrt(num1)
else:
st.error("Cannot take square root of a negative number.")
elif operation == "Logarithm":
if num1 > 0:
result = np.log(num1)
else:
st.error("Logarithm undefined for non-positive values.")
elif operation == "Sine":
result = np.sin(np.radians(num1))
elif operation == "Cosine":
result = np.cos(np.radians(num1))
elif operation == "Tangent":
result = np.tan(np.radians(num1))
except Exception as e:
st.error(f"An error occurred: {e}")
# Display result
if result is not None:
st.success(f"Result: {result}")