w3resource

JavaScript: Calculate the volume of a sphere

JavaScript DOM: Exercise-10 with Solution

Write a JavaScript program to calculate sphere volume.

Sample Output of the form :

volume sphere html form

Sample Solution: -

HTML Code:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Volume of a Sphere</title>
<style> 
body{padding-top:30px;}
label,input{display:block;} 
</style>
</head>
<body>
<p>Input radius value and get the volume of a sphere.</p>
<form action="" method="post" id="MyForm">
<label for="radius">Radius</label><input type="text" name="radius" id="radius" required>
<label for="volume">Volume</label><input type="text" name="volume" id="volume">
<input type="submit" value="Calculate" id="submit">    </form>
</body>
</html>

JavaScript Code:

function volume_sphere()
 {
  var volume;
  var radius = document.getElementById('radius').value;
  radius = Math.abs(radius);
  volume = (4/3) * Math.PI * Math.pow(radius, 3);
  volume = volume.toFixed(4);
  document.getElementById('volume').value = volume;
  return false;
 } 
window.onload = document.getElementById('MyForm').onsubmit = volume_sphere;

Sample Output:

javascript-dom-exercise-10

Flowchart:

Flowchart: JavaScript - Calculate the volume of a sphere.

Live Demo:

See the Pen javascript-dom-exercise-10 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus

Previous: Write a JavaScript program to count and display the items of a dropdown list, in an alert window.
Next: Write a JavaScript program to display a random image (clicking on a button) from the following list.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.