import math import streamlit as st def scientific_calculator(): st.title("Scientific Calculator") st.sidebar.header("Select Operation") operations = [ "Addition", "Subtraction", "Multiplication", "Division", "Exponentiation", "Square Root", "Sine", "Cosine", "Tangent", "Logarithm (base 10)", "Natural Logarithm" ] operation = st.sidebar.selectbox("Choose an operation:", operations) if operation in ["Addition", "Subtraction", "Multiplication", "Division", "Exponentiation"]: num1 = st.number_input("Enter first number:", value=0.0) num2 = st.number_input("Enter second number:", value=0.0) if operation == "Addition": st.write(f"Result: {num1} + {num2} = {num1 + num2}") elif operation == "Subtraction": st.write(f"Result: {num1} - {num2} = {num1 - num2}") elif operation == "Multiplication": st.write(f"Result: {num1} × {num2} = {num1 * num2}") elif operation == "Division": if num2 == 0: st.error("Error: Division by zero!") else: st.write(f"Result: {num1} ÷ {num2} = {num1 / num2}") elif operation == "Exponentiation": st.write(f"Result: {num1} ^ {num2} = {num1 ** num2}") elif operation == "Square Root": num = st.number_input("Enter a number:", value=0.0) if num < 0: st.error("Error: Cannot calculate the square root of a negative number!") else: st.write(f"Result: √{num} = {math.sqrt(num)}") elif operation in ["Sine", "Cosine", "Tangent"]: angle = st.number_input("Enter the angle in degrees:", value=0.0) radians = math.radians(angle) if operation == "Sine": st.write(f"Result: sin({angle}) = {math.sin(radians)}") elif operation == "Cosine": st.write(f"Result: cos({angle}) = {math.cos(radians)}") elif operation == "Tangent": st.write(f"Result: tan({angle}) = {math.tan(radians)}") elif operation == "Logarithm (base 10)": num = st.number_input("Enter a number:", value=0.0) if num <= 0: st.error("Error: Logarithm undefined for non-positive numbers!") else: st.write(f"Result: log10({num}) = {math.log10(num)}") elif operation == "Natural Logarithm": num = st.number_input("Enter a number:", value=0.0) if num <= 0: st.error("Error: Natural logarithm undefined for non-positive numbers!") else: st.write(f"Result: ln({num}) = {math.log(num)}") if __name__ == "__main__": scientific_calculator()