Find the Volume of a Cuboid in C

CCBeginner
Practice Now

Introduction

In this lab, we will learn how to calculate the volume of a cuboid using C programming. The lab will guide you through the step-by-step process of reading the dimensions of the cuboid (length, width, and height) from the user, and then computing the volume using the formula: Volume = Length _ Width _ Height. Finally, the calculated volume will be printed to the console.

The lab covers the fundamental concepts of geometry calculations and demonstrates how to apply them in a practical C programming exercise.


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/UserInteractionGroup -.-> c/user_input("`User Input`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/output -.-> lab-435186{{"`Find the Volume of a Cuboid in C`"}} c/variables -.-> lab-435186{{"`Find the Volume of a Cuboid in C`"}} c/user_input -.-> lab-435186{{"`Find the Volume of a Cuboid in C`"}} c/math_functions -.-> lab-435186{{"`Find the Volume of a Cuboid in C`"}} end

Read Length, Width, Height

In this step, we will learn how to read the dimensions of a cuboid using C programming. We'll create a simple program that takes length, width, and height as input from the user.

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

cd ~/project
nano volume_cuboid.c

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

#include <stdio.h>

int main() {
    // Declare variables to store dimensions
    float length, width, height;

    // Prompt user to enter length
    printf("Enter the length of the cuboid: ");
    scanf("%f", &length);

    // Prompt user to enter width
    printf("Enter the width of the cuboid: ");
    scanf("%f", &width);

    // Prompt user to enter height
    printf("Enter the height of the cuboid: ");
    scanf("%f", &height);

    // Print the entered dimensions
    printf("Dimensions entered:\n");
    printf("Length: %.2f\n", length);
    printf("Width: %.2f\n", width);
    printf("Height: %.2f\n", height);

    return 0;
}

Let's compile and run the program:

gcc volume_cuboid.c -o volume_cuboid
./volume_cuboid

Example output:

Enter the length of the cuboid: 5
Enter the width of the cuboid: 3
Enter the height of the cuboid: 2
Dimensions entered:
Length: 5.00
Width: 3.00
Height: 2.00

Compute Volume = L _ W _ H

In this step, we will modify our previous C program to calculate the volume of the cuboid using the formula: Volume = Length _ Width _ Height.

Let's edit the existing file:

cd ~/project
nano volume_cuboid.c

Update the program with volume calculation:

#include <stdio.h>

int main() {
    // Declare variables to store dimensions and volume
    float length, width, height, volume;

    // Prompt user to enter length
    printf("Enter the length of the cuboid: ");
    scanf("%f", &length);

    // Prompt user to enter width
    printf("Enter the width of the cuboid: ");
    scanf("%f", &width);

    // Prompt user to enter height
    printf("Enter the height of the cuboid: ");
    scanf("%f", &height);

    // Calculate volume
    volume = length * width * height;

    // Print the dimensions and calculated volume
    printf("Dimensions entered:\n");
    printf("Length: %.2f\n", length);
    printf("Width: %.2f\n", width);
    printf("Height: %.2f\n", height);
    printf("Volume of the cuboid: %.2f cubic units\n", volume);

    return 0;
}

Compile and run the updated program:

gcc volume_cuboid.c -o volume_cuboid
./volume_cuboid

Example output:

Enter the length of the cuboid: 5
Enter the width of the cuboid: 3
Enter the height of the cuboid: 2
Dimensions entered:
Length: 5.00
Width: 3.00
Height: 2.00
Volume of the cuboid: 30.00 cubic units

Print the Volume

In this final step, we'll ensure that the volume is printed clearly and formatted for better readability. We'll modify our previous program to enhance the output presentation.

Let's edit the file one last time:

cd ~/project
nano volume_cuboid.c

Update the program with improved volume printing:

#include <stdio.h>

int main() {
    // Declare variables to store dimensions and volume
    float length, width, height, volume;

    // Prompt user to enter length
    printf("Cuboid Volume Calculator\n");
    printf("------------------------\n");
    printf("Enter the length of the cuboid: ");
    scanf("%f", &length);

    // Prompt user to enter width
    printf("Enter the width of the cuboid: ");
    scanf("%f", &width);

    // Prompt user to enter height
    printf("Enter the height of the cuboid: ");
    scanf("%f", &height);

    // Calculate volume
    volume = length * width * height;

    // Print detailed volume information
    printf("\nCalculation Results:\n");
    printf("-------------------\n");
    printf("Length:  %.2f units\n", length);
    printf("Width:   %.2f units\n", width);
    printf("Height:  %.2f units\n", height);
    printf("Volume:  %.2f cubic units\n", volume);

    return 0;
}

Compile and run the final version of the program:

gcc volume_cuboid.c -o volume_cuboid
./volume_cuboid

Example output:

Cuboid Volume Calculator
------------------------
Enter the length of the cuboid: 5
Enter the width of the cuboid: 3
Enter the height of the cuboid: 2

Calculation Results:
-------------------
Length:  5.00 units
Width:   3.00 units
Height:  2.00 units
Volume:  30.00 cubic units

Summary

In this lab, we learned how to read the dimensions of a cuboid (length, width, and height) using C programming. We then used the formula Volume = Length _ Width _ Height to calculate the volume of the cuboid. Finally, we printed the calculated volume to the console.

The key steps involved reading the dimensions from the user, performing the volume calculation, and displaying the result. This lab provides a basic introduction to working with variables and performing arithmetic operations in C.

Other C Tutorials you may like