calc calculator code example

Example 1: how to make a calculator inc++

// ONLY 29 LINES OF CODE IN TOTAL
// For better calculator scroll down on this post
#include <iostream>
using namespace std;
int main() {
	cout << "Please enter the first number: ";
	double n1 = 0;
	cin >> n1;
	cout << "Please enter an operator (+, -, *, /): ";
	char op = '0';
	cin >> op;
	cout << "Please enter a second number: ";
	double n2 = 0;
	cin >> n2;
	double answer = 0;
	switch (op) {
	case '+': 
		answer = n1 + n2;
		break;
	case '-':
		answer = n1 - n2;
		break;
	case '*': 
		answer = n1 * n2;
		break;
	case '/': 
		answer = n1 / n2;
		break;
	}
	cout << "Thanks for using my calculator your answer is: " << answer;
  
  // this is the better calculator:
  
  #include <iostream>
#include <Windows.h>
using namespace std;
int main() {
	char jim = 'j';
	do {
		
		cout << "Please enter the first number: ";
		double n1 = 0;
		cin >> n1;
		cout << "Please enter an operator (+, -, *, /): ";
		char op = '0';
		cin >> op;
		cout << "Please enter a second number: ";
		double n2 = 0;
		cin >> n2;
		double answer = 0;
		switch (op) {
		case '+':
			answer = n1 + n2;
			break;
		case '-':
			answer = n1 - n2;
			break;
		case '*':
			answer = n1 * n2;
			break;
		case '/':
			answer = n1 / n2;
			break;
		}
		cout << "Thanks for using my calculator your answer is: " << answer << " do you want to run the program again? (y or n) ";
		cin >> jim;

	} while (jim = 'y' && jim != 'n');
	if (jim = 'n') {
		cout << "Thanks for using my calculator here is sum epic music";
		Beep(329, 300);
		Beep(493, 300);
		Beep(698, 300);
		Beep(659, 600);

		Beep(783, 300);
		Beep(698, 300);
		Beep(659, 600);

		Beep(329, 100);
		Beep(493, 300);
		Beep(698, 300);
		Beep(659, 600);

		Beep(392, 250);
		Beep(440, 200);
		Beep(587, 300);

		Beep(349, 250);
		Beep(587, 500);

		Beep(329, 300);
		Beep(493, 300);
		Beep(698, 300);
		Beep(659, 600);

		Beep(783, 300);
		Beep(698, 300);
		Beep(659, 600);

		Beep(329, 100);
		Beep(493, 300);
		Beep(698, 300);
		Beep(659, 600);

		Beep(392, 250);
		Beep(440, 200);
		Beep(587, 300);

		Beep(349, 250);
		Beep(587, 400);
	}
	return 0;
}

Example 2: What is the code for a calculator

<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Calculator Made by Sanjith </title>
<link href="https://fonts.googleapis.com/css?family=Inconsolata" rel="stylesheet">
<style>
    * {
        font-family: 'Inconsolata', monospace;
        color: rgb(0, 0, 0);
    }
    body {
        background: linear-gradient(to right, rgb(24, 184, 168), rgb(118, 190, 50));;
    }
    .container {
        width: 320px;
        background: linear-gradient(to right, rgb(173, 58, 173), rgb(184, 63, 26));;
        margin: 120px auto;
    }
    table {
        width: 100%;
        border-color: #09646407;
        background: linear-gradient(to right, rgb(184, 24, 37), rgb(78, 13, 230));;
    }
    td {
        width: 25%;
        border-color: rgba(0, 0, 0, 0.363);
    }
    button {
        width: 100%;
        height: 70px;
        font-size: 24px;
        background: linear-gradient(to right, rgb(151, 11, 151), rgb(190, 94, 50));;      
        border: none;
    }
    #inputLabel {
        height: 120px;
        font-size: 40px;
        vertical-align: bottom;
        text-align: right;
        padding-right: 16px;
        background: linear-gradient(to right, rgb(4, 65, 88), rgb(226, 68, 47));;
    }
</style>
</head>
<body>
<div class="container">
    <table border="1" cellspacing="0">
        <tr>
            <td colspan="4" id="inputLabel">0</td>
        </tr>
        <tr>
            <td colspan="3"><button onclick="pushBtn(this);">AC</button></td>
            <td><button onclick="pushBtn(this);">/</button></td>
        </tr>
        <tr>
            <td><button onclick="pushBtn(this);">7</button></td>
            <td><button onclick="pushBtn(this);">8</button></td>
            <td><button onclick="pushBtn(this);">9</button></td>
            <td><button onclick="pushBtn(this);">*</button></td>
        </tr>
        <tr>
            <td><button onclick="pushBtn(this);">4</button></td>
            <td><button onclick="pushBtn(this);">5</button></td>
            <td><button onclick="pushBtn(this);">6</button></td>
            <td><button onclick="pushBtn(this);">-</button></td>
        </tr>
        <tr>
            <td><button onclick="pushBtn(this);">1</button></td>
            <td><button onclick="pushBtn(this);">2</button></td>
            <td><button onclick="pushBtn(this);">3</button></td>
            <td><button onclick="pushBtn(this);">+</button></td>
        </tr>
        <tr>
            <td colspan="2"><button onclick="pushBtn(this);">0</button></td>
            <td><button onclick="pushBtn(this);">.</button></td>
            <td><button onclick="pushBtn(this);">=</button></td>
        </tr>
    </table>
</div>
 
<script>
    var inputLabel = document.getElementById('inputLabel');
     
    function pushBtn(obj) {
         
        var pushed = obj.innerHTML;
         
        if (pushed == '=') {
            // Calculate
            inputLabel.innerHTML = eval(inputLabel.innerHTML);
             
        } else if (pushed == 'AC') {
            // All Clear
            inputLabel.innerHTML = '0';
             
        } else {
            if (inputLabel.innerHTML == '0') {
                inputLabel.innerHTML = pushed;
                 
            } else {
                inputLabel.innerHTML += pushed;   
            }
        }
    }
</script>
</body>
</html>

Tags:

Html Example