| import streamlit as st |
| import math |
|
|
| |
| st.title("🧮 Scientific Calculator") |
|
|
| |
| st.write("Perform various mathematical operations including basic arithmetic, logarithms, trigonometric functions, and more.") |
|
|
| |
| operation = st.selectbox( |
| "Select an operation:", |
| [ |
| "Addition", |
| "Subtraction", |
| "Multiplication", |
| "Division", |
| "Power", |
| "Square Root", |
| "Logarithm (base 10)", |
| "Natural Logarithm (ln)", |
| "Sine", |
| "Cosine", |
| "Tangent" |
| ] |
| ) |
|
|
| |
| if operation in ["Addition", "Subtraction", "Multiplication", "Division", "Power"]: |
| num1 = st.number_input("Enter the first number:", format="%.2f") |
| num2 = st.number_input("Enter the second number:", format="%.2f") |
| if st.button("Calculate"): |
| if operation == "Addition": |
| st.success(f"Result: {num1} + {num2} = {num1 + num2}") |
| elif operation == "Subtraction": |
| st.success(f"Result: {num1} - {num2} = {num1 - num2}") |
| elif operation == "Multiplication": |
| st.success(f"Result: {num1} * {num2} = {num1 * num2}") |
| elif operation == "Division": |
| if num2 != 0: |
| st.success(f"Result: {num1} / {num2} = {num1 / num2}") |
| else: |
| st.error("Error: Division by zero is not allowed.") |
| elif operation == "Power": |
| st.success(f"Result: {num1} ^ {num2} = {math.pow(num1, num2)}") |
|
|
| elif operation == "Square Root": |
| num = st.number_input("Enter the number:", format="%.2f") |
| if st.button("Calculate"): |
| if num >= 0: |
| st.success(f"Result: √{num} = {math.sqrt(num)}") |
| else: |
| st.error("Error: Square root of a negative number is not allowed.") |
|
|
| elif operation == "Logarithm (base 10)": |
| num = st.number_input("Enter the number:", format="%.2f") |
| if st.button("Calculate"): |
| if num > 0: |
| st.success(f"Result: log10({num}) = {math.log10(num)}") |
| else: |
| st.error("Error: Logarithm is only defined for positive numbers.") |
|
|
| elif operation == "Natural Logarithm (ln)": |
| num = st.number_input("Enter the number:", format="%.2f") |
| if st.button("Calculate"): |
| if num > 0: |
| st.success(f"Result: ln({num}) = {math.log(num)}") |
| else: |
| st.error("Error: Natural logarithm is only defined for positive numbers.") |
|
|
| elif operation in ["Sine", "Cosine", "Tangent"]: |
| angle = st.number_input("Enter the angle in degrees:", format="%.2f") |
| if st.button("Calculate"): |
| radians = math.radians(angle) |
| if operation == "Sine": |
| st.success(f"Result: sin({angle}) = {math.sin(radians)}") |
| elif operation == "Cosine": |
| st.success(f"Result: cos({angle}) = {math.cos(radians)}") |
| elif operation == "Tangent": |
| st.success(f"Result: tan({angle}) = {math.tan(radians)}") |