Exercise
Floating Point Sphere
Objective
Develop a Python program that calculates the surface area and volume of a sphere, given its radius (surface area = 4 * pi * radius squared; volume = 4/3 * pi * radius cubed).
Note: For floating-point numbers, you should use float(...)
Example Python Exercise
Show Python Code
import math # Import math module to use pi
# Prompt the user for the radius of the sphere
radius = float(input("Enter the radius of the sphere: "))
# Calculate the surface area and volume
surface_area = 4 * math.pi * radius**2
volume = (4/3) * math.pi * radius**3
# Display the results
print(f"Surface area: {surface_area:.2f} square units")
print(f"Volume: {volume:.2f} cubic units")
Output
Case 1:
Enter the radius of the sphere: 5
Surface area: 314.16 square units
Volume: 523.60 cubic units
Case 2:
Enter the radius of the sphere: 2.5
Surface area: 61.55 square units
Volume: 65.45 cubic units
Share this Python Exercise