write a program to calculate the volume of sphere
Answers
Volume of sphere - Python
As we are not given/mentioned in the Question only by which programming language we write the required program, therefore here we use python program to calculate the volume of sphere.
Let us take a look at what we need to do in this program.
First we will import the math package. We'll import the Math module by simply typing There are various other modules as well, another common one being that is widely used for statistics purposes.
After that, We would simply take in the user input of the radius of sphere and store them as float value.
We use function since user can also enter the decimal value, values could always have the possibility of being in decimals. If we use function as the data type and a user enters a decimal value or integers, the program would result in an error. To be on the safer side, we use as both integers and decimals are accepted.
As we know that, the formula of the volume of sphere.
Volume of the sphere is : V = 4/3 * π * r³
[Here, V is the volume of sphere, the value of π is 22/7 but here we will math.pi for π and r is the radius of sphere]
But here radius is in cube, so here we can use function to raise cube. You can also use Exponentiation operator or by simply multiply radius value by three times.
Finally, by simply using this formula we can calculate the volume of sphere. And we will print the volume of sphere by using with "{:.2f}", it will print only 2 decimal places.
The Python Program
# Python Program to calculate the volume of sphere
# import math package/module to use pi value
import math
# Take input of Radius of sphere
radius = float(input("Enter the radius: "))
# Volume of sphere = 4/3 * π * r³
volume = (4/3)*math.pi*pow(radius,3)
# Print out the volume of sphere
print("The volume of sphere is", "{:.2f}".format(volume), "cubic units.")
Sample run
If you run the given program then the output will be as follows:
Enter the radius: 5
The volume of sphere is 523.60 cubic units.
[Program finished]
Volume of sphere - Python
As we are not given/mentioned in the Question only by which programming language we write the required program, therefore here we use python program to calculate the volume of sphere.
Let us take a look at what we need to do in this program.
First we will import the math package. We'll import the Math module by simply typing \texttt{import math.}import math. There are various other modules as well, another common one being \texttt{statistics,}statistics, that is widely used for statistics purposes.
After that, We would simply take in the user input of the radius of sphere and store them as float value.
We use
function since user can also enter the decimal value, values could always have the possibility of being in decimals. If we use
function as the data type and a user enters a decimal value or integers, the program would result in an error. To be on the safer side, we use \tt{float()}float() as both integers and decimals are accepted.
As we know that, the formula of the volume of sphere.
Volume of the sphere is : V = 4/3 * π * r³
[Here, V is the volume of sphere, the value of π is 22/7 but here we will math.pi for π and r is the radius of sphere]
But here radius is in cube, so here we use
function to raise cube. You can also use Exponentiation operator or by simply multiply radius value by three times.
Finally, by simply using this formula we can calculate the volume of sphere. And we will print the volume of sphere by using \tt{str.format()}str.format() with "{:.2f}", it will print only 2 decimal places.
The Python Program
# Python Program to calculate the volume of sphere
# import math package/module to use pi value
import math
# Take input of Radius of sphere
radius = float(input("Enter the radius: "))
# Volume of sphere = 4/3 * π * r³
volume = (4/3)*math.pi*pow(radius,3)
# Print out the volume of sphere
print("The volume of sphere is", "{:.2f}".format(volume), "cubic units.")
Sample run
If you run the given program then the output will be as follows:
Enter the radius: 5
The volume of sphere is 523.60 cubic units.
[Program finished]