File size: 2,736 Bytes
bd70e40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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()