The volume of a sphere with radius r is 4/3π. Write a Python program to find the volume of spheres with radius 7cm, 12cm, 16cm, respectively.
Python Program to Find Volume of Spheres:
#Program to find volume of spheres #Volume of sphere = (4/3) × π × r³
pi = 3.14159
#For radius 7cm r = 7 volume = (4/3) * pi * r ** 3 print("Volume of sphere with radius 7cm:", volume)
#For radius 12cm r = 12 volume = (4/3) * pi * r ** 3 print("Volume of sphere with radius 12cm:", volume)
#For radius 16cm r = 16 volume = (4/3) * pi * r ** 3 print("Volume of sphere with radius 16cm:", volume)
Output: Volume of sphere with radius 7cm: 1436.75 Volume of sphere with radius 12cm: 7238.22 Volume of sphere with radius 16cm: 17157.28
Explanation
This question tests the student's ability to apply mathematical formulas in Python programming. Following the pattern shown in Program 5-3 of the textbook (area of rectangle example), students should define variables for radius and pi, apply the volume formula V = (4/3)πr³, and print results. The exponent operator ** is used for r³. The program calculates volumes for three different radii by reassigning the variable r each time.
Solution Steps
Step 1: Define the value of pi as 3.14159
Step 2: Assign radius value to variable r (first r = 7)
Step 3: Calculate volume using formula volume = (4/3) * pi * r ** 3
Step 4: Print the calculated volume
Step 5: Repeat Steps 2-4 for radii 12 and 16