Python Program to calculate the volume and area of Sphere

A sphere is a three-dimensional geometric figure where every point on its surface is equidistant from the center. We can calculate both the surface area and volume of spheres using mathematical formulas.

r Solid Sphere R r Hollow Sphere

Formulas

The mathematical formulas for calculating sphere properties are ?

Surface Area of sphere ? 4?r²

Volume of solid sphere ? (4/3)?r³

Volume of hollow sphere ? (4/3)?(R³ - r³)

Where R is the outer radius and r is the inner radius for hollow spheres.

Method 1: Using Mathematical Formulas

Let's calculate the area and volume of both solid and hollow spheres using the standard mathematical formulas ?

import math

# Define radii
outer_radius = 7
inner_radius = 5

# Surface area calculation (same for both solid and hollow)
surface_area = 4 * math.pi * inner_radius ** 2

# Volume of solid sphere using outer radius
volume_solid = (4/3) * math.pi * outer_radius ** 3

# Volume of hollow sphere
volume_hollow = (4/3) * math.pi * (outer_radius ** 3 - inner_radius ** 3)

# Display results
print(f"Surface area of sphere: {surface_area}")
print(f"Volume of solid sphere: {volume_solid}")
print(f"Volume of hollow sphere: {volume_hollow}")
Surface area of sphere: 314.1592653589793
Volume of solid sphere: 1436.7550240409338
Volume of hollow sphere: 1012.2963640434486

Method 2: Using Functions

We can organize our code better by creating functions to calculate sphere properties ?

import math

def sphere_surface_area(radius):
    """Calculate surface area of a sphere."""
    return 4 * math.pi * radius ** 2

def sphere_volume_solid(radius):
    """Calculate volume of a solid sphere."""
    return (4/3) * math.pi * radius ** 3

def sphere_volume_hollow(outer_radius, inner_radius):
    """Calculate volume of a hollow sphere."""
    return (4/3) * math.pi * (outer_radius ** 3 - inner_radius ** 3)

# Example usage
radius = 6
outer_r = 8
inner_r = 4

print(f"Surface area (r={radius}): {sphere_surface_area(radius):.2f}")
print(f"Solid volume (r={radius}): {sphere_volume_solid(radius):.2f}")
print(f"Hollow volume (R={outer_r}, r={inner_r}): {sphere_volume_hollow(outer_r, inner_r):.2f}")
Surface area (r=6): 452.39
Solid volume (r=6): 904.78
Hollow volume (R=8, r=4): 1608.50

Method 3: Interactive Calculator

Here's a complete sphere calculator that handles user input and provides comprehensive results ?

import math

class SphereCalculator:
    def __init__(self):
        self.pi = math.pi
    
    def calculate_all(self, radius, inner_radius=None):
        """Calculate all sphere properties."""
        results = {}
        
        # Surface area
        results['surface_area'] = 4 * self.pi * radius ** 2
        
        # Solid volume
        results['solid_volume'] = (4/3) * self.pi * radius ** 3
        
        # Hollow volume if inner radius provided
        if inner_radius:
            results['hollow_volume'] = (4/3) * self.pi * (radius ** 3 - inner_radius ** 3)
            results['material_volume'] = results['hollow_volume']
        
        return results

# Example calculations
calc = SphereCalculator()

# Solid sphere
solid_results = calc.calculate_all(5)
print("Solid Sphere (radius=5):")
print(f"  Surface Area: {solid_results['surface_area']:.2f}")
print(f"  Volume: {solid_results['solid_volume']:.2f}")

print()

# Hollow sphere
hollow_results = calc.calculate_all(7, 3)
print("Hollow Sphere (outer=7, inner=3):")
print(f"  Surface Area: {hollow_results['surface_area']:.2f}")
print(f"  Material Volume: {hollow_results['hollow_volume']:.2f}")
Solid Sphere (radius=5):
  Surface Area: 314.16
  Volume: 523.60

Hollow Sphere (outer=7, inner=3):
  Surface Area: 615.75
  Material Volume: 1320.25

Comparison of Methods

Method Best For Advantages
Direct Formula Simple calculations Straightforward, easy to understand
Functions Reusable code Modular, organized, reusable
Class-based Complex applications Object-oriented, extensible

Conclusion

Python makes sphere calculations simple using the math library for ? and basic arithmetic operations. Use functions for reusable code and classes for more complex sphere calculation applications.

Updated on: 2026-03-26T22:30:49+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements