import streamlit as st import math # App title st.set_page_config(page_title="Advanced Calculator", page_icon="🧮") st.title("🧮 Advanced Calculator") st.write("An advanced calculator with percentage & history") # Initialize history in session state if "history" not in st.session_state: st.session_state.history = [] # Select operation operation = st.selectbox( "Choose an operation", [ "Addition (+)", "Subtraction (-)", "Multiplication (*)", "Division (/)", "Power (x^y)", "Modulus (%)", "Square Root (√x)", "Percentage (x % of y)" ] ) # Inputs if operation == "Square Root (√x)": num = st.number_input("Enter number", value=0.0) elif operation == "Percentage (x % of y)": x = st.number_input("Enter percentage value", value=0.0) y = st.number_input("Enter total value", value=0.0) else: num1 = st.number_input("Enter first number", value=0.0) num2 = st.number_input("Enter second number", value=0.0) # Calculate button if st.button("Calculate"): try: if operation == "Addition (+)": result = num1 + num2 record = f"{num1} + {num2} = {result}" elif operation == "Subtraction (-)": result = num1 - num2 record = f"{num1} - {num2} = {result}" elif operation == "Multiplication (*)": result = num1 * num2 record = f"{num1} * {num2} = {result}" elif operation == "Division (/)": if num2 == 0: st.error("Division by zero is not allowed") st.stop() result = num1 / num2 record = f"{num1} / {num2} = {result}" elif operation == "Power (x^y)": result = num1 ** num2 record = f"{num1} ^ {num2} = {result}" elif operation == "Modulus (%)": result = num1 % num2 record = f"{num1} % {num2} = {result}" elif operation == "Square Root (√x)": if num < 0: st.error("Cannot calculate square root of negative number") st.stop() result = math.sqrt(num) record = f"√{num} = {result}" elif operation == "Percentage (x % of y)": result = (x / 100) * y record = f"{x}% of {y} = {result}" # Display result st.success(f"Result: {result}") # Save history st.session_state.history.append(record) except Exception as e: st.error(f"Error: {e}") # Show history st.subheader("📜 Calculation History") if st.session_state.history: for h in st.session_state.history: st.write(h) else: st.info("No calculations yet")