Determine the Volume of a Sphere in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to determine the volume of a sphere using C programming. The lab consists of two main steps: reading the radius of the sphere and then computing the volume using the formula V = (4.0/3.0) _ π _ r³. You will create a simple program that prompts the user to input the radius, calculates the volume, and displays the result. By the end of this lab, you will have a better understanding of geometry calculations and how to implement them in C.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/constants("`Constants`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435173{{"`Determine the Volume of a Sphere in C`"}} c/variables -.-> lab-435173{{"`Determine the Volume of a Sphere in C`"}} c/constants -.-> lab-435173{{"`Determine the Volume of a Sphere in C`"}} c/user_input -.-> lab-435173{{"`Determine the Volume of a Sphere in C`"}} c/math_functions -.-> lab-435173{{"`Determine the Volume of a Sphere in C`"}} end

Read the Radius

In this step, you will learn how to read the radius of a sphere using C programming. We'll create a simple program that prompts the user to input the radius and stores it in a variable.

First, let's create a new C file in the ~/project directory:

cd ~/project
nano sphere_volume.c

Now, let's write the code to read the radius:

#include <stdio.h>

int main() {
    // Declare a variable to store the radius
    double radius;

    // Prompt the user to enter the radius
    printf("Enter the radius of the sphere: ");

    // Read the radius from user input
    scanf("%lf", &radius);

    // Print the entered radius to confirm
    printf("Radius entered: %.2f\n", radius);

    return 0;
}

Example output:

Enter the radius of the sphere: 5.5
Radius entered: 5.50

Let's break down the code:

  • double radius; declares a variable to store the radius as a floating-point number
  • printf() displays a prompt for the user to enter the radius
  • scanf() reads the user's input and stores it in the radius variable
  • %.2f formats the output to display two decimal places

Compile the program:

gcc sphere_volume.c -o sphere_volume

Example output:

labex@ubuntu:~/project$ gcc sphere_volume.c -o sphere_volume

Now you can run the program:

./sphere_volume

Compute Volume = (4.0/3.0)PI

In this step, you will learn how to calculate the volume of a sphere using the mathematical formula V = (4.0/3.0) _ π _ r³. We'll modify the previous program to include the volume calculation.

Open the existing file and update the code:

cd ~/project
nano sphere_volume.c

Replace the previous code with the following:

#include <stdio.h>
#include <math.h>

int main() {
    // Declare variables
    double radius, volume;

    // Constant for PI
    const double PI = 3.14159265358979323846;

    // Prompt the user to enter the radius
    printf("Enter the radius of the sphere: ");

    // Read the radius from user input
    scanf("%lf", &radius);

    // Calculate the volume using the sphere volume formula
    volume = (4.0 / 3.0) * PI * pow(radius, 3);

    // Print the radius and calculated volume
    printf("Radius: %.2f\n", radius);
    printf("Volume of the sphere: %.2f\n", volume);

    return 0;
}

Compile the updated program:

gcc sphere_volume.c -o sphere_volume -lm

Example output:

labex@ubuntu:~/project$ gcc sphere_volume.c -o sphere_volume -lm

Note the -lm flag, which links the math library needed for the pow() function.

Run the program:

./sphere_volume

Example output:

Enter the radius of the sphere: 5.5
Radius: 5.50
Volume of the sphere: 696.46

Let's break down the key changes:

  • Added #include <math.h> to use the pow() function
  • Defined a constant PI for precise calculations
  • Used the formula volume = (4.0 / 3.0) * PI * pow(radius, 3)
  • pow(radius, 3) calculates r³
  • Printed both radius and calculated volume

Print the Volume

In this final step, we'll enhance the output formatting to make the volume calculation more presentable and user-friendly.

Open the existing file:

cd ~/project
nano sphere_volume.c

Update the code with improved formatting:

#include <stdio.h>
#include <math.h>

int main() {
    // Declare variables
    double radius, volume;

    // Constant for PI
    const double PI = 3.14159265358979323846;

    // Prompt the user to enter the radius
    printf("Sphere Volume Calculator\n");
    printf("------------------------\n");
    printf("Enter the radius of the sphere: ");

    // Read the radius from user input
    scanf("%lf", &radius);

    // Calculate the volume using the sphere volume formula
    volume = (4.0 / 3.0) * PI * pow(radius, 3);

    // Print formatted output
    printf("\nCalculation Results:\n");
    printf("-------------------\n");
    printf("Radius:        %.2f units\n", radius);
    printf("Volume:        %.2f cubic units\n", volume);

    // Additional descriptive output
    printf("\nNote: Volume is calculated using the formula V = (4/3) * π * r³\n");

    return 0;
}

Compile the program:

gcc sphere_volume.c -o sphere_volume -lm

Example output when running the program:

Sphere Volume Calculator
------------------------
Enter the radius of the sphere: 5.5

Calculation Results:
-------------------
Radius:        5.50 units
Volume:        696.46 cubic units

Note: Volume is calculated using the formula V = (4/3) * π * r³

Key improvements:

  • Added descriptive headers
  • Formatted output with units
  • Included a note about the calculation formula
  • Improved readability of the results

Run the program to verify:

./sphere_volume

Summary

In this lab, you will learn how to read the radius of a sphere from user input and calculate its volume using the formula V = (4.0/3.0) _ π _ r³. First, you will declare a variable to store the radius and prompt the user to enter the value. Then, you will compute the volume of the sphere using the mathematical formula and the constant value of π. Finally, you will print the calculated volume to the console.

Other C Tutorials you may like