This Python program defines a class named Sphere that receives the radius of a sphere and calculates both its surface area and volume.
The calculations are based on the following formulas:
- Surface Area = 4 × π × r²
- Volume = (4/3) × π × r³
import math
# Define the Sphere class
class Sphere:
def __init__(self, radius):
self.r = radius
def surface_area(self):
return 4 * math.pi * self.r ** 2
def volume(self):
return (4 / 3) * math.pi * self.r ** 3
# Receive radius from user
radius = float(input("Enter the radius of the sphere: "))
# Create Sphere object and calculate results
s = Sphere(radius)
print("Surface Area:", round(s.surface_area(), 2))
print("Volume:", round(s.volume(), 2))
Enter the radius of the sphere: 3
Surface Area: 113.1
Volume: 113.1
- The Sphere class stores the radius in self.r
- The surface_area() method calculates 4 × π × r²
- The volume() method calculates (4/3) × π × r³
- The results are rounded to two decimal places and printed