How to create a advance Calculator ?

SEO Help and Tips

How to create a advance Calculator ?

Here is the example code of advance colorful canculator:

HTML Code:

<head>
    <title>Colorful Calculator</title>
    <style>
        body {
            background-color: #f5f5f5;
            font-family: Arial, sans-serif;
        }

        .calculator {
            width: 300px;
            margin: 0 auto;
            background-color: #333;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
            color: white;
            text-align: center;
        }

        input[type="text"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: none;
            border-radius: 5px;
            font-weight: bold;
            font-size: 18px;
            color: white;
            background-color: #444;
        }

        button {
            width: 60px;
            height: 60px;
            font-size: 18px;
            margin: 4px;
            border: none;
            border-radius: 50%;
            background-color: #666;
            color: white;
            cursor: pointer;
            font-weight: bold;
        }

        button.operator {
            background-color: #f7931e;
        }

        button.clear {
            background-color: #e74c3c;
        }

        button.equal {
            background-color: #2ecc71;
        }

        button.memory {
            background-color: #3498db;
        }
    </style>
</head>
<body>
    <div class="calculator">
        <input type="text" id="result" disabled>
        <button class="memory" onclick="appendToResult('M')">M</button>
        <button class="clear" onclick="clearResult()">C</button>
        <button onclick="appendToResult('%')">%</button>
        <button class="operator" onclick="appendToResult('+')">+</button>
        <br>
        <button onclick="appendToResult('7')">7</button>
        <button onclick="appendToResult('8')">8</button>
        <button onclick="appendToResult('9')">9</button>
        <button class="operator" onclick="appendToResult('-')">-</button>
        <br>
        <button onclick="appendToResult('4')">4</button>
        <button onclick="appendToResult('5')">5</button>
        <button onclick="appendToResult('6')">6</button>
        <button class="operator" onclick="appendToResult('*')">*</button>
        <br>
        <button onclick="appendToResult('1')">1</button>
        <button onclick="appendToResult('2')">2</button>
        <button onclick="appendToResult('3')">3</button>
        <button class="operator" onclick="appendToResult('/')">/</button>
        <br>
        <button onclick="appendToResult('0')">0</button>
        <button class="equal" onclick="calculateResult()">=</button>
        <button onclick="calculateSquareRoot()">√</button>
        <button onclick="calculateCos()">cos</button>
    </div>

    <script>
        function appendToResult(value) {
            document.getElementById('result').value += value;
        }

        function clearResult() {
            document.getElementById('result').value = '';
        }

        function calculateResult() {
            try {
                const expression = document.getElementById('result').value;
                const result = eval(expression);
                document.getElementById('result').value = result;
            } catch (error) {
                document.getElementById('result').value = 'Error';
            }
        }

        function calculateSquareRoot() {
            try {
                const expression = document.getElementById('result').value;
                const result = Math.sqrt(eval(expression));
                document.getElementById('result').value = result;
            } catch (error) {
                document.getElementById('result').value = 'Error';
            }
        }

        function calculateCos() {
            try {
                const expression = document.getElementById('result').value;
                const result = Math.cos(eval(expression));
                document.getElementById('result').value = result;
            } catch (error) {
                document.getElementById('result').value = 'Error';
            }
        }
    </script>
</body>

Python:

import math

def append_to_result(value):
    global result
    result += value

def clear_result():
    global result
    result = ''

def calculate_result():
    global result
    try:
        result = str(eval(result))
    except:
        result = 'Error'

def calculate_square_root():
    global result
    try:
        result = str(math.sqrt(eval(result)))
    except:
        result = 'Error'

def calculate_cos():
    global result
    try:
        result = str(math.cos(eval(result)))
    except:
        result = 'Error'

result = ''

while True:
    print("Options:")
    print("Enter 'M' for memory")
    print("Enter 'C' to clear")
    print("Enter '%' for percentage")
    print("Enter '+' for addition")
    print("Enter '-' for subtraction")
    print("Enter '*' for multiplication")
    print("Enter '/' for division")
    print("Enter '=' to calculate")
    print("Enter '√' for square root")
    print("Enter 'cos' for cosine")
    print("Enter 'quit' to end the program")

    user_input = input(": ")

    if user_input == 'quit':
        break
    elif user_input in ('M', 'C', '%', '+', '-', '*', '/', '=', '√', 'cos'):
        if user_input == 'M':
            append_to_result('M')
        elif user_input == 'C':
            clear_result()
        elif user_input == '=':
            calculate_result()
        elif user_input == '√':
            calculate_square_root()
        elif user_input == 'cos':
            calculate_cos()
        else:
            append_to_result(user_input)
    else:
        print("Invalid input")

    print("Result:", result)

Comments

Popular posts from this blog

How to fix SSL Certificate Issues?

How to Fix Website Mixed Content Issues?

How to Fix Mobile Responsiveness Issues?

Popular posts from this blog

How to fix SSL Certificate Issues?

How to Fix Website Mixed Content Issues?

How to Fix Mobile Responsiveness Issues?