In: Computer Science
Write a defining table and a computer program that computes and outputs the volume of a torus with inner radius a and outer radius b. A doughnut is an example of a torus. Your program must read the inner radius and outer radius from two text fields and display the volume in a div. The formula for the volume of a torus is
v = π2(a + b)(a - b)2
4
where v is the volume, π is the constant pi, a is the inner radius, and b is the outer radius. Your program should allow a user to enter real numbers such as 7.54. If you wish, you may use the following HTML code in your program.
<!DOCTYPE HTML>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Volume of a Torus</title>
<script>
// Your JavaScript function belongs here.
</script>
</head>
<body>
<h1>Volume of a Torus</h1>
Inner radius <input type="text" id="inner"><br>
Outer radius <input type="text" id="outer"><br>
<button type="button" onclick="computeVolume()">Volume</button><br>
Volume <div id="volume"></div>
</body>
</html>
| Test Cases | ||
|---|---|---|
| Inputs | Output | |
| inner | outer | |
| 2.8 | 3.1 | 1.31 |
| 14.6 | 17.1 | 488.85 |
<!DOCTYPE HTML>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Volume of a Torus</title>
<script>
function computeVolume() {
var a = Number(document.getElementById("outer").value);
var b = Number(document.getElementById("inner").value);
var vol = Math.pow(Math.PI, 2) * (a + b) * Math.pow(a - b, 2) / 4;
document.getElementById("volume").innerHTML = vol.toFixed(2);
}
</script>
</head>
<body>
<h1>Volume of a Torus</h1>
Inner radius <input type="text" id="inner"><br>
Outer radius <input type="text" id="outer"><br>
<button type="button" onclick="computeVolume()">Volume</button>
<br>
Volume
<div id="volume"></div>
</body>
</html>
