Computer Science, asked by shashiranibanti069, 3 months ago

write a program to calculate the volume of sphere​

Answers

Answered by Anonymous
4

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.} There are various other modules as well, another common one being \texttt{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 \tt{float()} function since user can also enter the decimal value, values could always have the possibility of being in decimals. If we use \tt{int()} 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()} 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 \pmb{\tt{pow()}} 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()} with "{:.2f}", it will print only 2 decimal places.

\rule{300}{1}

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.")

\rule{300}{1}

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]

Answered by juwairiyahimran18
0

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

 \tt{float()}float()

function since user can also enter the decimal value, values could always have the possibility of being in decimals. If we use

\tt{int()}int()

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

\pmb{\tt{pow()}}

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]

Similar questions