C Program to Calculate Surface Area and Volume of a Sphere

Surface Area and Volume of a Sphere
Surface Area and Volume of a Sphere

A sphere is a three-dimensional object where every point on its surface is equidistant from its center. The properties of a sphere include Surface Area and Volume, which can be calculated if we know its radius.

The formulas to calculate these properties are:

  • Surface Area = 4 π r2
  • Volume = (4 / 3) π r3

Where:

  • r is the radius of the sphere.
  • π (Pi) is approximately equal to 3.14159.


Code


#include <stdio.h>
#define PI 3.14159

// Function to calculate surface area and volume of a sphere
void calculateSphereProperties(double radius) {
    double surfaceArea, volume;
    
    // Calculate surface area
    surfaceArea = 4 * PI * radius * radius;
    
    // Calculate volume
    volume = (4.0 / 3.0) * PI * radius * radius * radius;
    
    // Display results
    printf("Surface Area of Sphere: %.2f\n", surfaceArea);
    printf("Volume of Sphere: %.2f\n", volume);
}

int main() {
    double radius;
    
    // Input radius from the user
    printf("Enter the radius of the sphere: ");
    scanf("%lf", &radius);
    
    // Function call to calculate properties
    calculateSphereProperties(radius);
    
    return 0;
}
    


Output

Enter the radius of the sphere: 5
Surface Area of Sphere: 314.16
Volume of Sphere: 523.60


Explanation

  • Input: The user enters the radius of the sphere.
  • Surface Area Calculation: The surface area is calculated using the formula 4 π r2.
  • Volume Calculation: The volume is calculated using the formula (4 / 3) π r3.
  • Output: The program then displays the surface area and volume, each formatted to two decimal places for clarity.