Introduction
In this lab, you will learn how to calculate the surface area of a cylinder using C programming. The lab covers the steps to read the radius and height of the cylinder from the user, and then compute the surface area using the formula 2PIr*(r+h). The program will then print the calculated surface area.
The lab provides a complete step-by-step guide, including the necessary C code, to help you understand the process of computing the surface area of a cylinder. By the end of this lab, you will have a solid understanding of how to perform geometry calculations using C programming.
Read Radius and Height
In this step, you will learn how to read the radius and height of a cylinder using C programming. We'll create a simple program that prompts the user to input these values.
First, let's create a new C file in the project directory:
cd ~/project
nano cylinder_surface_area.c
Now, add the following code to the file:
#include <stdio.h>
int main() {
float radius, height;
// Prompt user to enter radius
printf("Enter the radius of the cylinder: ");
scanf("%f", &radius);
// Prompt user to enter height
printf("Enter the height of the cylinder: ");
scanf("%f", &height);
// Print the entered values
printf("Radius: %.2f\n", radius);
printf("Height: %.2f\n", height);
return 0;
}
Let's break down the code:
- We use
printf()to display prompts for radius and height scanf()is used to read float values for radius and height%.2fformats the output to display two decimal places
Compile and run the program:
gcc cylinder_surface_area.c -o cylinder_surface_area
./cylinder_surface_area
Example output:
Enter the radius of the cylinder: 5
Enter the height of the cylinder: 10
Radius: 5.00
Height: 10.00
Compute Surface Area = 2PIr*(r+h)
In this step, you will modify the previous program to calculate the surface area of a cylinder using the formula: Surface Area = 2PIr*(r+h).
Open the existing file and update the code:
cd ~/project
nano cylinder_surface_area.c
Replace the previous code with the following:
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, height, surface_area;
// Prompt user to enter radius
printf("Enter the radius of the cylinder: ");
scanf("%f", &radius);
// Prompt user to enter height
printf("Enter the height of the cylinder: ");
scanf("%f", &height);
// Calculate surface area
surface_area = 2 * PI * radius * (radius + height);
// Print the results
printf("Radius: %.2f\n", radius);
printf("Height: %.2f\n", height);
printf("Surface Area: %.2f\n", surface_area);
return 0;
}
Let's break down the changes:
- Added
#define PI 3.14159to define a constant for π - Created a
surface_areavariable to store the calculated result - Used the formula: Surface Area = 2PIr*(r+h)
- Added a print statement to display the calculated surface area
Compile and run the program:
gcc cylinder_surface_area.c -o cylinder_surface_area
./cylinder_surface_area
Example output:
Enter the radius of the cylinder: 5
Enter the height of the cylinder: 10
Radius: 5.00
Height: 10.00
Surface Area: 628.32
Print the Surface Area
In this final step, you will enhance the program to provide a more user-friendly output of the cylinder's surface area.
Open the existing file and make final modifications:
cd ~/project
nano cylinder_surface_area.c
Update the code with formatted output:
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, height, surface_area;
// Prompt user to enter radius
printf("Cylinder Surface Area Calculator\n");
printf("--------------------------------\n");
printf("Enter the radius of the cylinder: ");
scanf("%f", &radius);
// Prompt user to enter height
printf("Enter the height of the cylinder: ");
scanf("%f", &height);
// Calculate surface area
surface_area = 2 * PI * radius * (radius + height);
// Print formatted results
printf("\nCalculation Results:\n");
printf("--------------------------------\n");
printf("Radius: %.2f units\n", radius);
printf("Height: %.2f units\n", height);
printf("Surface Area: %.2f square units\n", surface_area);
return 0;
}
Compile and run the program:
gcc cylinder_surface_area.c -o cylinder_surface_area
./cylinder_surface_area
Example output:
Cylinder Surface Area Calculator
--------------------------------
Enter the radius of the cylinder: 5
Enter the height of the cylinder: 10
Calculation Results:
--------------------------------
Radius: 5.00 units
Height: 10.00 units
Surface Area: 628.32 square units
Key improvements:
- Added a title for the calculator
- Included separators for better readability
- Added units to the output
- Formatted the results with clear labels
Summary
In this lab, you learned how to read the radius and height of a cylinder using C programming, and then calculate the surface area of the cylinder using the formula: Surface Area = 2PIr*(r+h). The program prompts the user to enter the radius and height, and then calculates and displays the surface area.
The key steps covered in this lab are: 1) reading the radius and height from the user, and 2) computing the surface area using the provided formula.



