How to Calculate Volume and Surface Area of Geometric Shapes by C Language

calculate volume and surface area by c programing language

Introduction :

In the field of geometry, calculating the volume and surface area of various geometric shapes is a fundamental task in mathematics and engineering. By applying the necessary mathematical formulas and algorithms, you can develop efficient and robust programs that calculating the volume and surface area of various shapes including cuboid, cubes, cylinders, spheres, hemisphere, cones and more.

In this article I shall explore how to calculate the volume and surface area of common geometric shapes such as cuboid, cube, cylinder, sphere, hemisphere, cone, frustum of a cone and right parallelepiped using the C programing language. Here, I discuss the formulas for each shape and create programs to solve real-world problems using these formulas. To run the following program, I use VS Code on my pc. You can install VS Code on your pc from here.

What is volume and surface area :

Before seeing the following programs of calculating the volume and surface area of different geometrical shapes, you have to understand basic concepts of volume and surface area.

The amount of space occupying by a three-dimensional object is called volume. It is a measurement of the total volume or the amount of material that can fit within the edge of the object. The unit of volume is usually expressed in cubic units such as cubic meters or cubic centimeters.

The total area (outer surface) covered by the surface of a three-dimensional object is called surface area. It measures the amount of material required to cover the object. The unit of surface area is usually expressed in square units such as square meters or square centimeters.

Both volume and surface area are important concepts in geometry, used to analyze various objects in many fields such as architecture, engineering, physics and manufacturing. Calculating these values gives us valuable information about the size, capacity and properties of different shapes and objects.

Calculate the volume, surface area and diagonal of cuboid by C programming language :

A cuboid is a three-dimensional shape with six rectangular faces.

The volume of a cuboid represents the amount of space enclosed by the cuboid. The volume of a cuboid is calculated by multiplying its length, width and height. The formula for the volume of a cuboid is given below.

volume of cuboid = l x w x h

The surface area of a cuboid refers to the total area of its outer surfaces. The surface area of a cuboid is the sum of the areas of its six faces. The following formula is used for calculating surface area of a cuboid.

surface area of cuboid = 2(lw + lh + wh)

The diagonal of cuboid is the line segment connecting two opposite corners. The diagonal of a cuboid can calculate using the Pythagorean theory. The formula for diagonal of a cuboid is given below.

diagonal of cuboid = √(l² + w² + h²)

Note that l, w and h stand for length, width, and height of the cuboid. For calculating the volume, surface area and diagonal of a cuboid, you have to know the length, width and height of the cuboid.

In the program, I show you how calculate the volume, surface area and diagonal of a cuboid using C programing language. At first, include header files. You have to include stdio.h for input/output function and math.h for square root function. Next, take six float variable such as “length”, “width”, “height”, “volume”, “surface_area” and “diagonal”.

Then, ask the user for entering the length, width, and height of the cuboid by printf() function. By the scanf() function store value of length, width and height in “length”, “width” and “height” variable respectively. After that, calculate the volume, surface area and diagonal of the cuboid and store in “volume”, “surface_area” and “diagonal” variable respectively. Finally, print the calculated volume, surface area and diagonal of a cuboid on the console by printf() function.

/* Developd by Puskar Jasu*/
#include <stdio.h>
#include <math.h>
int main()
{
    float length, width, height, volume, surface_area, diagonal;
    printf("Enter the length of the cuboid: ");
    scanf("%f", &length);
    printf("Enter the width of the cuboid: ");
    scanf("%f", &width);
    printf("Enter the height of the cuboid: ");
    scanf("%f", &height);
    volume = length * width * height;
    surface_area = 2 * (length * width + width * height + height * length);
    diagonal = sqrt(length * length + width * width + height * height);
    printf("Volume, surface area and diagonal of the cuboid are %.2f, %.2f and %.2f\n", volume, surface_area, diagonal);
    return 0;
}

Output :

After running this program, you can see the following output of calculating the volume, surface area and diagonal of cuboid by C programming language.

output of calculating the volume, surface area and diagonal of cuboid by C programming language

Calculate the volume, surface area and diagonal of cube by C programming language :

A cube is a three-dimensional shape, that have six equal square faces.

The volume of a cube refers to the sum of space it holds. The volume of a cube is calculated by the length of one side to the power of 3. The following formula is used for calculating the volume of a cube.

volume of cube = (side)3

The surface area of a cube is total areas of its six faces. The surface area of a cube is determined by multiplying the area of one face by 6, as a cube has six equal square faces. The following formula is used for calculating the surface area of a cube.

surface area of cube = 6 x side2

The diagonal of a cube is the line portion connecting two opposite corners of the cube. The diagonal of a cube can be calculated using the Pythagorean theory. The following formula is used for calculating the diagonal of a cube.

diagonal of cube = 3 x side

Where side stand for the length of any side of the cube. All sides of a cube are equal, so calculating the volume, surface area and diagonal of a cube, you need to know the length of any side.

In the program, I show you how calculate the volume, surface area and diagonal of a cube using C programing language. At first, you have to include header files such as stdio.h for input/output function and math.h for square root function. Now, take four float variable such as “side”, “volume”, “surface_area”and “diagonal”.

Next, ask the user for enters the side of the cube by printf() function. Using the scanf() function store value of side in “side” variable. After calculating the volume, surface area and diagonal of the cuboid, store all values in “volume”, “surface_area” and “diagonal” variable respectively. Finally, display the calculated volume, surface area and diagonal of a cube on the screen by printf() function.

/* Developd by Puskar Jasu*/
#include <stdio.h>
#include <math.h>
int main()
{
    float side, volume, surface_area, diagonal;
    printf("Enter the length of side of the cube: ");
    scanf("%f", &side);
    volume = side * side * side;
    surface_area = 6 * side * side;
    diagonal = sqrt(3) * side;
    printf("Volume, surface area and diagonal of the cube are %.2f, %.2f and %.2f\n", volume, surface_area, diagonal);
    return 0;
}

Output :

When you run this program on your pc, you see the output of calculating the volume, surface area and diagonal of cube by C programming language like below image.

output of calculating the volume, surface area and diagonal of cube by C programming language

Calculate the volume, curved surface area and total surface area of cylinder by C programming language :

A cylinder is a three-dimensional geometrical shape with two parallel rounded bases connected by a curved surface.

The volume of a cylinder represents the amount of space it can hold. It can be calculated by multiplying the area of the base (circle) by the height of the cylinder. The following formula is used for calculating the volume of a cylinder.

volume of cylinder = πr2h

The curved surface area of a cylinder refers to the area of the lateral surface that connects the two rounded bases. It can be calculated by multiplying the circumference of the base by the height of the cylinder. You can calculate the curved surface area using the following formula.

curved surface area of cylinder = 2πrh

The total surface area of a cylinder represents the sum of all its surface areas, including the two rounded bases and the curved surface. It can be calculated by adding the area of the two bases and the curved surface area. You can calculate the total surface area using the following formula.

total surface area of cylinder = 2πrh + 2πr2

Where r and h stand for radius and height. To calculate the volume, curved surface area and the total surface area of a cylinder, you have to know the radius and height of the cylinder.

In the program, I show you how calculate the volume, curved surface area and total surface area of a cylinder using C programing language. At first, you have to include the necessary header files stdio.h for input/output function. Now, take five float variable such as “radius”, “height”, “volume”, “curved_surface_area” and “total_surface_area”.

Next, ask the user for entering the radius and height of the cylinder by printf() function. Using the scanf() function stores the value of radius and height in “radius” and “height” variable respectively. After calculating the volume, curved surface area and total surface area of the cylinder, store all values in “volume”, “curved_surface_area” and “total_surface_area” variables respectively. At last, display the calculated volume, curved surface area and total surface area of a cylinder on the screen by printf() function.

/* Developd by Puskar Jasu*/
#include <stdio.h>
int main()
{
    float radius, height, volume, curved_surface_area, total_surface_area;
    printf("Enter the radius of the cylinder ");
    scanf("%f", &radius);
    printf("Enter the height of the cylinder ");
    scanf("%f", &height);
    volume = 3.14159 * radius * radius * height;
    curved_surface_area = 2 * 3.14159 * radius * height;
    total_surface_area = 2 * 3.14159 * radius * (radius + height);
    printf("Volume, curved surface area and total surface area of a cylinder are %.2f, %.2f and %.2f\n", volume, curved_surface_area, total_surface_area);
    return 0;
}

Output :

After running this program, you can see the following output of calculating the volume, curved surface area and total surface area of cylinder by C programming language.

output of calculating the volume, curved surface area and total surface area of cylinder by C programming language.

Calculate the volume and surface area of sphere by C programming language :

A sphere is a three-dimensional geometrical form that is perfectly round and symmetrical in every direction.

A sphere’s volume refers to how much space it occupies. It can be computed by multiplying the cube of the radius by a constant value (4/3π). The following formula is used to compute the volume of a sphere.

volume of sphere = (4/3)πr3

The surface area of a sphere represents the total area of its outer surface. It can be calculated by multiplying the square of the radius by a constant value (4π). The following formula is used to compute the surface area of a sphere.

surface area of sphere = 4πr2

In the program, I show you how calculate the volume and surface area of a sphere using C programing language. At first, you have to include the necessary header files stdio.h for input/output function. Now, take three float variable such as “radius”, “volume” and “surface_area”.

Next, ask the user for entering the radius of the sphere by printf() function. Using the scanf() function store value of radius in “radius” variable. After calculating the volume and surface area of the sphere, store all values in “volume” and “surface_area” variables respectively. Lastly, display the calculated volume and surface area of a sphere on the screen by printf() function.

/* Developd by Puskar Jasu*/
#include <stdio.h>
int main()
{
    float radius, volume, surface_area;
    printf("Enter the radius of the sphere ");
    scanf("%f", &radius);
    volume = (4.0 / 3.0) * 3.14159 * radius * radius * radius;
    surface_area = 4.0 * 3.14159 * radius * radius;
    printf("Volume and surface area of the sphere are %.2f and %.2f\n", volume, surface_area);
    return 0;
}

Output :

You can see the following output of calculating the volume and surface area of sphere by C programming language.

output of calculating the volume and surface area of sphere by C programming language.

Calculate the volume, curved surface area and total surface area of hemisphere by C programming language :

A hemisphere is a three-dimensional geometric shape that is half of a sphere.

The volume of a hemisphere refers to the amount of space it occupies. It can be calculated by multiplying the cube of the radius by a constant value (2/3)π. You can calculate the volume of a hemisphere using the following formula.

volume of hemisphere = (2/3)πr3

The curved surface area of a hemisphere represents the area covered by its curved surface. It can be calculated by multiplying the square of the radius by a constant value 2π. You can calculate the curved surface area of a hemisphere using the following formula.

curved surface area of hemisphere = 2πr2

The total surface area of a hemisphere represents the sum of the curved surface area and the area of the circular base. It can be obtained by adding the curved surface area to the area of the base, which is equal to half the surface area of a sphere. You can calculate the total surface area of a hemisphere using the following formula.

total surface area of a hemisphere = 3πr2

In the program, I show you how calculate the volume, curved surface area and total surface area of hemisphere by C programming language. At first, you have to include the necessary header files stdio.h for input/output function. Now, take four float variable such as “radius”, “volume”, “curved_surface_area” and “total_surface_area”.

Next, ask the user for entering the radius of the hemisphere by printf() function. Using the scanf() function store the value of radius in “radius” variable. After calculating the volume, curved surface area and total surface area of hemisphere, store all values in “volume”, “curved_surface_area” and “total_surface_area” variables respectively. Lastly, display the calculated volume, curved surface area and total surface area of hemisphere on the screen by printf() function.

/* Developd by Puskar Jasu*/
#include <stdio.h>
int main()
{
    float radius, volume, curved_surface_area, total_surface_area;
    printf("Enter the radius of the hemisphere ");
    scanf("%f", &radius);
    volume = (2.0 / 3.0) * 3.14159 * radius * radius * radius;
    curved_surface_area = 2.0 * 3.14159 * radius * radius;
    total_surface_area = 3.0 * 3.14159 * radius * radius;
    printf("Volume, curved surface area and total surface area of a hemisphere are %.2f, %.2f and %.2f\n", volume,
           curved_surface_area, total_surface_area);
    return 0;
}

Output :

To run this program, you can see the following output of calculating the volume, curved surface area and total surface area of a hemisphere by C programming language.

output of calculating the volume, curved surface area and total surface area of a hemisphere by C programming language.

Calculate the volume, curved surface area and total surface area of cone by C programming language :

A cone is a three-dimensional geometrical shape with a rounded base and a pointed top.

The volume of a cone refers to the amount of space it holds. It can be calculated by one third of the value of multiplying the area of the base by the height of the cone. The following formula is used for calculating the volume of a cone.

volume of cone = (1/3)πr2h

The curved surface area of a cone represents the total area covered by its slanted surface(without base). The curved surface area is calculated by multiplying the circumference of the base by the slant height of the cone. The following formula is used for calculating the curved surface area of a cone.

curved surface area of cone = πrl

The total surface area of a cone refers to the sum of the areas of its curved surface and the base. It can be obtained by adding the curved surface area to the area of the circular base. The following formula is used for calculating the total surface area of a cone.

total surface area of a cone = πrl + πr2

In the program, I show you how calculate the volume, curved surface area and total surface area of cone by C programming language. At first, you have to include the necessary header files stdio.h for input/output function. Now, take six float variable such as “radius”, “height”, “slant_height”, “volume”, “curved_surface_area” and “total_surface_area”.

Next, ask the user for entering the radius, height and slant_height of the cone by printf() function. Using the scanf() function stores the value of radius, height and slant_height in “radius”, “height” and “slant_height” variables respectively. After calculating the volume, curved surface area and total surface area of a cone, store all values in “volume”, “curved_surface_area” and “total_surface_area” variables respectively. Lastly, display the calculated volume, curved surface area and total surface area of cone on the screen by printf() function.

/* Developd by Puskar Jasu*/
#include <stdio.h>
int main()
{
    float radius, height, slant_height, volume, curved_surface_area, total_surface_area;
    printf("Enter the radius of the cone ");
    scanf("%f", &radius);
    printf("Enter the height of the cone ");
    scanf("%f", &height);
    printf("Enter the slant height of the cone ");
    scanf("%f", &slant_height);
    volume = (1.0 / 3.0) * 3.14159 * radius * radius * height;
    curved_surface_area = 3.14159 * radius * slant_height;
    total_surface_area = curved_surface_area + 3.14159 * radius * radius;
    printf("Volume, curved surface area and total surface area of a cone are %.2f, %.2f and %.2f\n", volume,
           curved_surface_area, total_surface_area);
    return 0;
}

Output :

After running this program, you can see the following output of calculating the volume, curved surface area and total surface area of a cone by C programming language.

output of calculating the volume, curved surface area and total surface area of a cone by C programming language.

Calculate the volume, curved surface area and total surface area of fructum of a cone by C programming language :

A frustum of a cone is a three-dimensional geometric shape that same as a cone with its top portion removed.

The volume of a frustum of a cone refers to the amount of space it holds. It can be calculated by multiplying the average of the areas of the top and bottom bases by the height of the frustum and dividing it by 3. The formula for calculating volume of a frustum of a cone is given below.

volume of frustum of a cone = (1/3)πh(r12 + r22 + r1 x r2)

The curved surface area of a frustum of a cone refers to the total area covered by its curved surface. It can be calculated by finding the slant height of the frustum and multiplying it by the average of the perimeters of the two bases. The formula for calculating curved surface area of a frustum of a cone is given below.

curved surface area of frustum of a cone = π(r1 + r2)l

The total surface area of a frustum of a cone represents the sum of the areas of its curved surface, top base and bottom base. It can be obtained by adding the curved surface area to the areas of the top and bottom bases. The formula for calculating curved surface area of a frustum of a cone is given below.

total surface area of frustum of a cone = π(r1 + r2)l + π(r12 + r22)

Where r1, r2, h and l are bottom radius, top radius, height and slant height of the frustum of the cone.

In the program, I show you how calculate the volume, curved surface area and total surface area of the frustum of cone by C programming language. At first, you have to include the necessary header files stdio.h for input/output function. Now, take seven float variable such as “radius1”, “radius2”, “height”, “slant_height”, “volume”, “curved_surface_area” and “total_surface_area”.

Now, ask the user for entering the bottom and top radius, height and slant_height of the frustum of a cone by printf() function. Using the scanf() function stores the value of bottom and top radius, height and slant_height in “radius1”, “radius2”, “height” and “slant_height” variables respectively. After calculating the volume, curved surface area and total surface area of a cone, store all values in “volume”, “curved_surface_area” and “total_surface_area” variables respectively. Lastly, display the calculated volume, curved surface area and total surface area of the frustum of cone on the screen by printf() function.

/* Developd by Puskar Jasu*/
#include <stdio.h>
int main()
{
    float radius1, radius2, height, slant_height, volume, curved_surface_area, total_surface_area;
    printf("Enter the bottom radius of the frustum of a cone ");
    scanf("%f", &radius1);
    printf("Enter the top radius of the frustum of a cone ");
    scanf("%f", &radius2);
    printf("Enter the height of the frustum of a cone ");
    scanf("%f", &height);
    printf("Enter the slant height of the frustum of a cone ");
    scanf("%f", &slant_height);
    volume = (1.0 / 3.0) * 3.14159 * height * (radius1 * radius1 + radius2 * radius2 + radius1 * radius2);
    curved_surface_area = 3.14159 * (radius1 + radius2) * slant_height;
    total_surface_area = curved_surface_area + 3.14159 * (radius1 * radius1 + radius2 * radius2);
    printf("Volume, curved surface area and total surface area of frustum of a cone are %.2f, %.2f and %.2f\n", volume, curved_surface_area, total_surface_area);
    return 0;
}

Output :

To run this program, you can see the following output of calculating the volume, curved surface area and total surface area of a frustum of a cone by C programming language.

output of calculating the volume, curved surface area and total surface area of a frustum of a cone by C programming language.

Calculate the volume and surface area of right parallelepiped by C programming language :

A right parallelepiped is a three-dimensional shape with six faces where side face is rectangular and base or top face is parallelogram.

The volume of a right parallelepiped represents the amount of space enclosed by the right parallelepiped. The volume of a right parallelepiped is calculated by multiplying its base area with the height of right parallelepiped. The formula for the volume of a right parallelepiped is given below.

volume of right parallelepiped = base area x h
base area of right parallelepiped = 2√s(s-l)(s-w)(s-d)

The surface area of a right parallelepiped refers to the total area of its outer surfaces. The surface area of a right parallelepiped is the sum of the areas of its six faces. The following formula is used for calculating surface area of a right parallelepiped.

surface area of right parallelepiped = 2h(l + w) + 4√s(s-l)(s-w)(s-d)

Note that l, w, h and d stand for length, width, height and any base diagonal of the right parallelepiped.
s = (l + w + d)/2
For calculating the volume, surface area and diagonal of a right parallelepiped, you have to know the length, width, height and any base diagonal of the right parallelepiped.

In the program, I show you how calculate the volume, surface area and diagonal of a right parallelepiped using C programing language. At first, include header files. You have to include stdio.h for input/output function and math.h for square root function. Next, take seven float variable such as “length”, “width”, “height”, “diagonal”, “volume”, “surface_area” and “s”.

Then, ask the user for entering the length, width, height and any diagonal of base face of the right parallelepiped by printf() function. By the scanf() function store value of length, width and height in “length”, “width”, “height” and “diagonal” variable respectively. After that, calculate the volume and surface area of the right parallelepiped and store in “volume” and “surface_area” variable respectively. Finally, print the calculated volume and surface area of a right parallelepiped on the console by printf() function.

/* Developd by Puskar Jasu*/
#include <stdio.h>
#include <math.h>
int main()
{
    float length, width, diagonal, height, volume, surface_area, s;
    printf("Enter the length of the right parallelepiped: ");
    scanf("%f", &length);
    printf("Enter the width of the right parallelepiped: ");
    scanf("%f", &width);
    printf("Enter the height of the right parallelepiped: ");
    scanf("%f", &height);
    printf("Enter the any base diagonal of the right parallelepiped: ");
    scanf("%f", &diagonal);
    s = (length + width + diagonal) / 2;
    volume = 2 * sqrt(s * (s - length) * (s - width) * (s - diagonal)) * height;
    surface_area = 2 * height * (length + width) + 4 * sqrt(s * (s - length) * (s - width) * (s - diagonal));
    printf("Volume, surface area and diagonal of the right parallelepiped are %.2f and %.2f\n", volume, surface_area);
    return 0;
}

Output :

To run this program, you can see the following output of calculating the volume and surface area right parallelepiped by C programming language.

output of calculating the volume and surface area right parallelepiped by C programming language.

Conclusion :

Throughout this article, I have covered the calculations for several common geometric shapes including the cuboid, cube, cylinder, sphere, hemisphere, cone, frustum of a cone and right parallelepiped using C programing language. Here, I also show you the appropriate formulas for calculating the volume and surface area of various geometrical shapes. After reading this article, you grain a basic knowledge about calculating volume and surface area of these geometrical shapes. Thank you for visiting my site.

Scroll to Top