C Program to find surface area and volume of a cube

For a better understanding, we always recommend you to learn the basic topics of C programming listed below:

What is surface area? How is it calculated for a cube?

In this c program, we have to calculate the surface area and volume of a cube. The surface area is generally the area of all the surfaces of an object. For calculating the surface area of a cube, we need to calculate the area of six surfaces of the cube. The area to find one surface of the cube is 'a*a'. To calculate six sides, we have to multiply six with 'a*a' hence 6*a*a.

What is Volume? How is it Calculated?

The Volume of an object is a three-dimensional property. So usually, Volume will be the area in three dimensions. In our case, we can calculate the volume of the cube by using the formula "side*side*side." Here C program uses a pow() function to calculate the Volume of the cube to calculate the Volume by side raise to three quickly.

Suppose the side of a Cube is "a" cm. Then, the Volume of the Cube is a*a*a, i.e., a^3. We can calculate Surface Area by 6*a*a that is 6a^2. In this C program, we use the formula.

  • Surface Area = 6*side*side
  • Volume = pow(side,3)

Why we are using math.h Header file?

Here we are using the 'math.h' Header file to use the function 'pow()' .'Math.h' Header file contains several mathematical operations. We are using the pow() function to find out the power of a number.

How we implement the calculation of surface area and volume in C?

For implementing the program,  declare the variables 'surfArea', 'volume,' and 'side' as Float type. Read the length of the side from the user using the scanf function and store it in the variable 'side'. Then we will calculate the Surface Area and Volume by using the formula. Display the Surface Area and the Volume using the printf function.

ALGORITHM

STEP 1: Include the Header files to use the built-in functions in the C program.

STEP 2: Declare the variables 'side', 'surfArea', 'volume' as Float type.

STEP 3: Read the length of a side from the user into the variable side

STEP 4: Calculate the Surface Area by using the formula 'surfArea=6.0 * side * side'.

STEP 5: Calculate the Volume by using the formula 'volume=pow(side,3)'.

STEP 6: Display the Surface Area as 'surfArea' and Volume as 'volume.'

C Source Code

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

void main() {
  float side, surfArea, volume;
  printf("Enter the length of a side\n"); /* user input the length of a side */
  scanf("%f", & side);
  surfArea = 6.0 * side * side; /* surface area calculation */
  volume = pow(side, 3);
  printf("Surface area = %6.2f and Volume = %6.2f\n", surfArea, volume); /* displaying the output surface area and volume */
}
                                      

OUTPUT

Enter the length of a side

4

Surface area =  96.00 and Volume =  64.00

RUN2

Enter the length of a side

12.5

Surface area = 937.50 and Volume = 1953.12