Reputation: 1548
I'm trying to calculate the volume of a sphere, with floating points, but I couldn't quite understand the logic behind the exercise.
Write a program that calculates and displays the volume of a sphere, providing the value of its radius (R). The formula for calculating volume is: (4/3) * pi * R3. Consider (assign) to pi the value 3.14159.
const PI = 3.14159;
let R = parseFloat(gets());
//TODO: Fill in the blanks with a possible solution to the challenge
print("VOLUME = " + );
Upvotes: 0
Views: 950
Reputation: 1548
Reading a lot of the tips here, I solved it, I don't know if it's the best way, but I solved it.
const PI = 3.14159;
let R = parseFloat(gets());
var raio = parseFloat(R);
var volumeEsfera = (4/3) * PI * Math.pow(raio, 3);
print("VOLUME = " + volumeEsfera.toFixed(3) );
Upvotes: 1
Reputation: 131
You should try by yourself, but here is the solution and explanation.
If you are using javascript on the browser:
let radius = parseFloat(prompt("Enter value for radius: "));
let volume = 4/3 * Math.PI * Math.pow(radius, 3);
console.log("The volume is: ", volume);
We are requesting the user input with prompt() and assigning it to radius variable. We calculate the volume with the equation v = 4/3 πr³
, Math.PI represents the number PI and Math.pow() calculates the given base (this case, radius) taken to the power of the given exponent. Last step is to print the result to the output, which is done with console.log()
Upvotes: 2