JavaScript program to find the volume of a sphere

JavaScript program to find the volume of a sphere:

In this post, we will learn how to find the volume of a sphere in JavaScript. With this program, you will learn how to do basic mathematical calculations in JavaScript.

The volume of a sphere is also called the capacity of the sphere. To find the volume, we need the radius of the sphere. It can be calculated with a small formula if we know the radius of the sphere.

The following formula is used to find the volume of a sphere:

Volume = 4/3 * πr^3

Where,

  • r is the radius of the sphere.
  • π or PI is a mathematical constant. It is defined in the Math class in JavaScript. We can create one variable initialized with the value of π or we can simply use the Math class.

In this post, we will learn different ways to solve this problem. Let me show you one by one.

Method 1: Finding the sphere volume with a given radius value:

Let’s try to find the volume of a sphere with a given radius value. The value of the radius is given and this program will simply print the volume of the sphere.

let radius = 5;
let volume = (4/3)* Math.PI * Math.pow(radius, 3);

console.log('Volume of Sphere: '+volume.toFixed(2));

Download the code on GitHub

Here,

  • The radius of the sphere is assigned to the variable radius.
  • The variable volume is used to assign the calculated volume. It uses the same formula we discussed above to calculate the volume.
  • Finally, the last line is printing this value. It formats the volume to two digits after the decimal.

If you run this program, it will print the below output:

Volume of Sphere: 523.60

Method 2: Finding the sphere volume with a user-given radius value:

We can also take the radius as input from the user. The other part of the program will be similar to the previous example. It will take the radius from the user and calculate the volume. The following program shows how to find the volume of a sphere with any user-provided radius value:

let radius = parseInt(prompt("Enter the radius", "0"), 0);
let volume = (4/3)* Math.PI * Math.pow(radius, 3);

console.log('Volume of Sphere: '+volume.toFixed(2));

Download the code on GitHub

You need to run it in a browser. Go to the console tab in the developer menu and paste the code. It will ask the user to enter the radius value with a prompt. Enter it and hit ok, it will show the volume in the console.

You will get similar results as the above example.

JavaScript program to find the volume of a sphere

Method 3: How to find the sphere of a volume with HTML, CSS, and JavaScript:

We can also use the JavaScript code with an HTML page to get the radius in an input component. Create one file index.html with the following code:

<input type="number" id="input_radius" placeholder="Enter the radius" />
<input type="button" value="Find" id="input_button" />
<script>
  function calculateVolume() {
    let radius = document.getElementById("input_radius").value;

    let volume = (4 / 3) * Math.PI * Math.pow(radius, 3);

    alert("The volume of a sphere: " + volume.toFixed(2));
  }

  let button = document.getElementById("input_button");
  button.onclick = calculateVolume;
</script>

Download the code on GitHub

  • This is an html file with the JavaScript code included to find the volume. If you open this file in a browser, it will show one input box with a button. This input box is to get the radius and the button is to calculate that value.

  • If you enter any value in that input box and click on the button, it will show one alert dialog with the calculated volume of the sphere.

javascript find the volume of a sphere

You can add css to style the input box and the button if you want.

You might also like: