C Program to Find Surface Area and Volume of a Cylinder
Introduction
Cylinders are three-dimensional shapes with two parallel circular bases connected by a curved surface. They are commonly used in various real-world applications, such as pipes, cans, and tanks. Understanding how to calculate the surface area and volume of a cylinder is essential for many engineering, construction, and design tasks.
In this tutorial, we will learn how to calculate these properties using a C program. The surface area determines how much material is needed to cover the cylinder, while the volume measures the space it occupies.
Formulas for Surface Area and Volume
The calculations for surface area and volume depend on two key inputs:
- Radius (r): The distance from the center to the edge of the circular base.
- Height (h): The vertical distance between the two bases.
Here are the formulas:
-
Surface Area: The total surface area of a cylinder consists of:
- The area of the two circular bases:
2 × Ï€ × r²
- The curved surface area:
2 × Ï€ × r × h
Total Surface Area = 2 × Ï€ × r × (r + h)
- The area of the two circular bases:
-
Volume: The volume is calculated as the product of the area of the base and the height:
Volume = Ï€ × r² × h
In the program, we will use 3.14159
as the value of π for simplicity.
C Program Code
Here is the C program to compute the surface area and volume of a cylinder:
#include <stdio.h> #define PI 3.14159 int main() { float radius, height, surface_area, volume; // Input radius and height of the cylinder printf("Enter the radius of the cylinder: "); scanf("%f", &radius); printf("Enter the height of the cylinder: "); scanf("%f", &height); // Calculate surface area and volume surface_area = 2 * PI * radius * (radius + height); volume = PI * radius * radius * height; // Display the results printf("Surface Area of the cylinder: %.2f\n", surface_area); printf("Volume of the cylinder: %.2f\n", volume); return 0; }
Output
Here is an example of the output when the program is executed:
Enter the radius of the cylinder: 3 Enter the height of the cylinder: 5 Surface Area of the cylinder: 150.80 Volume of the cylinder: 141.37
The program calculates the total surface area and volume based on the inputs provided by the user. The results are displayed with two decimal places for better readability.
Explanation of the Program
Let’s break down the program to understand its logic and structure:
-
Input: The program uses the
scanf
function to take two inputs from the user: the radius and height of the cylinder. These values are stored in the variablesradius
andheight
. -
Constants: The value of π is defined as a constant using the
#define
preprocessor directive. This makes the code cleaner and more efficient. -
Calculations:
- The surface area is computed using the formula
2 × Ï€ × r × (r + h)
, and the result is stored in the variablesurface_area
. - The volume is computed using the formula
Ï€ × r² × h
, and the result is stored in the variablevolume
.
- The surface area is computed using the formula
-
Output: The
printf
statements display the calculated values of surface area and volume. The%.2f
format specifier ensures that the results are shown with two decimal points. -
Return Value: The
return 0;
statement indicates that the program executed successfully.