All programs
Web Technology Lab · BCSL504 Web Tech (HTML · CSS · JS)

Problem Statement Program 6

Apply HTML, CSS and JavaScript to design a Simple Calculator to perform the following operations:

Sum (+)
Difference (-)
Product (*)
Quotient (/)
Remainder (%)
Power
Square Root
Square

Web Tech (HTML · CSS · JS) Code Source

index.html
<!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{
        document.getElementById("result").value =
        eval(document.getElementById("result").value);
    }
    catch{
        alert("Invalid Expression");
        clearResult();
    }
}

</script>

</body>
</html>

Output 1 screenshot

Download the clean, watermark-free output image — ₹1.00
Protected content · © VTU Developer · Unauthorised distribution is prohibited