Membuat Kalkulator Sederhana

Membuat Kalkulator Sederhana

Berikut adalah contoh kode untuk membuat kalkulator sederhana menggunakan HTML, CSS, dan JavaScript:

HTML
<!DOCTYPE html>
<html>
<head>
    <title>Kalkulator Sederhana</title>
    <style>
        .container {
            width: 300px;
            margin: 0 auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        .input-group {
            margin-bottom: 10px;
        }
        .input-group label {
            display: block;
            margin-bottom: 5px;
        }
        .input-group input {
            width: 100%;
            padding: 5px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }
        .btn {
            display: inline-block;
            padding: 6px 12px;
            margin-bottom: 0;
            font-size: 14px;
            font-weight: normal;
            line-height: 1.42857143;
            text-align: center;
            white-space: nowrap;
            vertical-align: middle;
            cursor: pointer;
            border: 1px solid transparent;
            border-radius: 4px;
            color: #fff;
            background-color: #337ab7;
            border-color: #2e6da4;
        }
        .btn:hover {
            background-color: #286090;
            border-color: #204d74;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>Kalkulator Sederhana</h2>
        <div class="input-group">
            <label for="num1">Angka 1:</label>
            <input type="number" id="num1">
        </div>
        <div class="input-group">
            <label for="num2">Angka 2:</label>
            <input type="number" id="num2">
        </div>
        <div class="input-group">
            <button class="btn" onclick="tambah()">Tambah</button>
            <button class="btn" onclick="kurang()">Kurang</button>
            <button class="btn" onclick="kali()">Kali</button>
            <button class="btn" onclick="bagi()">Bagi</button>
        </div>
        <div class="input-group">
            <label for="result">Hasil:</label>
            <input type="text" id="result" readonly>
        </div>
    </div>

    <script>
        function tambah() {
            var num1 = parseFloat(document.getElementById('num1').value);
            var num2 = parseFloat(document.getElementById('num2').value);
            var result = num1 + num2;
            document.getElementById('result').value = result;
        }

        function kurang() {
            var num1 = parseFloat(document.getElementById('num1').value);
            var num2 = parseFloat(document.getElementById('num2').value);
            var result = num1 - num2;
            document.getElementById('result').value = result;
        }

        function kali() {
            var num1 = parseFloat(document.getElementById('num1').value);
            var num2 = parseFloat(document.getElementById('num2').value);
            var result = num1 * num2;
            document.getElementById('result').value = result;
        }

        function bagi() {
            var num1 = parseFloat(document.getElementById('num1').value);
            var num2 = parseFloat(document.getElementById('num2').value);
            var result = num1 / num2;
            document.getElementById('result').value = result;
        }
    </script>
</body>
</html>
HTML

Output:

Kalkulator Sederhana

Kalkulator Sederhana

Silakan gunakan kode di atas untuk membuat kalkulator sederhana menggunakan HTML, CSS, dan JavaScript. Anda dapat menyesuaikan tampilan dan fungsi kalkulator sesuai kebutuhan Anda.