Question #38968

write a program that computes the tax and tip on a restaurant bill. The program should ask the user to enter the charge for the meal. The tax should be 6.75 percent of the meal charge. The tip should be 15 percent of the total after adding the tax. Display the meal charge, tax amount, tip amount, and total bill on the screen.

Expert's answer

Answer on Question#38968 – Charge calculation program – Programming, AJAX | JavaScript | HTML | PHP

Below are two independent solutions with same functionality written with PHP and Javascript.

PHP:


<!DOCTYPE html>
<html>
<head>
<title></title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<form id="charge" method="post" method="post">
Please input charge:&nbsp;<input id="charge" name="charge" yr="submit" submit=""/>
</form>
<?php
if (isset($_POST['charge'])) {
    $charge = $_POST['charge'];
    $tax = round($charge * 6.75 / 100, 2);
    $tip = round(($charge + $tax) * 15 / 100, 2);
    $total = $charge + $tax + $tip;
    echo "meal charge: " . $_POST['charge'] . "<br>";
    echo "tax amout: " . $tax . "<br>";
    echo "tip: " . $tip . "<br>";
    echo "<hr>";
    echo "total: " . $total;
}
?>
</body>
</html>


Javascript:


<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=windows-1251" http-equiv="Content-Type">
<script>
window.onload = function() {
    var charge = prompt("please enter charge");
    var tax = (charge * 6.75 / 100).toFixed(2);
    var tip = ((charge * 1 + tax * 1) * 15 / 100).toFixed(2);
    var total = charge * 1 + tax * 1 + tip * 1;
    var output = document.getElementById('result');
    output.innerHTML = "charge: " + charge + "<br>" +
        "tax:" + tax + "<br>" +
        "tip: " + tip + "<br>" +
        "total: " + total;
};
</script>
</head>
<body>
<div id="result">
</div>
</body>
</html>

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS