Web Technology lab

Program 6

Apply HTML, CSS and JavaScript to design a simple calculator to perform the following operations: sum, product, difference, remainder, quotient, power, square-root and square.



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Calculator</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: black;
            color: #ecf0f1;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .calculator {
            background-color: #111;
            border: 1px solid white;
            padding: 20px;
            border-radius: 15px;
            box-shadow: 0 10px 20px rgba(0, 0, 0, 0.5);
            width: 320px;
        }

        #result {
            width: 94%;
            height: 60px;
            font-size: 1.5em;
            padding: 10px;
            margin-bottom: 15px;
            border: none;
            border-radius: 10px;
            background-color: #7d7b7b;
            color: black;
            text-align: right;
        }

        .buttons {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 10px;
        }

        input[type="button"] {
            width: 100%;
            height: 60px;
            font-size: 1.2em;
            border: 1px solid gray;
            border-radius: 10px;
            background-color: #111;
            color: #ecf0f1;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        input[type="button"]:hover {
            background-color: #1e1d1d;
        }

        input[type="button"]:active {
            background-color: #1e1d1d;
        }

        .equal {
            background-color: #e74c3c;
        }

        .equal:hover {
            background-color: #c0392b;
        }

        .clear {
            background-color: #f39c12;
        }

        .clear:hover {
            background-color: #e67e22;
        }
    </style>
</head>
<body>

    <div class="calculator">
        <h1>Simple Calculator</h1>
        <input type="text" id="result" disabled>

        <div class="buttons">
            <input type="button" value="1" onclick="append('1')">
            <input type="button" value="2" onclick="append('2')">
            <input type="button" value="3" onclick="append('3')">
            <input type="button" value="+" onclick="append('+')">

            <input type="button" value="4" onclick="append('4')">
            <input type="button" value="5" onclick="append('5')">
            <input type="button" value="6" onclick="append('6')">
            <input type="button" value="-" onclick="append('-')">

            <input type="button" value="7" onclick="append('7')">
            <input type="button" value="8" onclick="append('8')">
            <input type="button" value="9" onclick="append('9')">
            <input type="button" value="*" onclick="append('*')">

            <input type="button" value="0" onclick="append('0')">
            <input type="button" value="C" class="clear" onclick="clearResult()">
            <input type="button" value="=" class="equal" onclick="calculate()">
            <input type="button" value="/" onclick="append('/')">
        </div>
    </div>

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

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

        function calculate() {
            try {
                let result = eval(document.getElementById('result').value);
                document.getElementById('result').value = result;
            } catch (error) {
                alert('Invalid Expression');
                clearResult();
            }
        }
    </script>

</body>
</html>

               


  

Output :-

output
Comments

Replay !

0 Comments

Share Your Thoughts

Please enter your name
Please enter a valid email
Password must be at least 6 characters
Please enter your comment
Email Verification Required
We've sent a 6-digit verification code to . Please enter the code below to verify your email address.
Email Verified Successfully!
Your email has been verified. Would you like to proceed with posting your comment?

Type "YES" to confirm and post your comment, or click Cancel to skip posting.