The volume V of a sphere is four-thirds times pi times the radius cubed.
The formula for the volume of a sphere is V=4/3πr3.
Where r is the radius of the sphere. In the figure below ,enter the the radius of the sphere and note how the formula is used to calculate the volume. Since the 4, 3 and pi are constants, this simplifies to approximately
Output:
JavaScript Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<input type="text" id="radius" placeholder="Enter the radius of a sphere"> <input type="button" value="Calculate" id="btnCalc" > <script> function calc(){ let number=document.getElementById("radius").value; number=Number(number); // The formula for the volume of a sphere (4. π. r*r*r )/ 3 let sphere=(4*Math.PI*number*number*number)/3; //Cut the floating digits to two float sphere =sphere .toFixed(2); alert("The volume of a sphere: "+sphere); } let btnCalc=document.getElementById("btnCalc"); btnCalc.onclick=calc; </script> |