Floating Point Sphere - Python Programming Exercise

This Python program calculates both the surface area and volume of a sphere based on its radius. It uses the formulas for surface area (4 * pi * radius squared) and volume (4/3 * pi * radius cubed) to compute the required values. The program makes use of the mathematical constant pi from the math module, and the radius is taken as a floating-point number to ensure accurate calculations for non-integer values. The radius is inputted by the user, and the program outputs the surface area and volume of the sphere. This program is an excellent example of applying mathematical formulas to practical problems and demonstrates how to handle floating-point numbers in Python. The result helps users understand the relationship between the radius of a sphere and its surface area and volume, making it a great tool for learning about geometry and programming. This task enhances skills in working with user input, performing arithmetic calculations, and utilizing mathematical constants in Python to solve real-world problems.

 Category

Python Data Types

 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

 Copy 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

 More Python Programming Exercises of Python Data Types

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Vowel Check - Conditional

    This Python program prompts the user to input a symbol and categorizes it as a vowel (if it’s a lowercase vowel), a digit, or any other symbol. T...

  •  Northeast Triangle

    This Python program prompts the user to enter a width and then displays a triangle pattern where the number of stars decreases with each row, and underscores (...

  •  Prime Divisors

    This Python program prompts the user to enter a number and then displays that number as a product of its prime factors. The program uses a factorization approa...

  •  Conditional Symbols

    This Python program prompts the user to enter a symbol and determines whether it is an uppercase vowel, a lowercase vowel, a digit, or any other ...

  •  Character Loop

    This Python program uses a "for" loop to print the uppercase letters from "B" to "N". By utilizing Python's range() function and specifying the A...

  •  Double Precision Pi Approximation

    This Python program calculates an approximation for PI using the series expression: pi/4 = 1/1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 ... The program allows t...