Introduction
In this lab, you will learn how to compute the circumference of a circle using C programming. The lab covers the step-by-step process of reading the radius from the user, performing the circumference calculation, and displaying the result. You will learn how to use the mathematical formula for circumference, as well as how to handle user input and output in a C program.
The lab provides a complete example, including the necessary code and explanations, to guide you through the process of calculating the circumference of a circle. By the end of the lab, you will have a solid understanding of how to perform geometry calculations using C programming.
Read the Radius
In this step, we will learn how to read the radius of a circle in C programming. Reading user input is a fundamental skill in programming that allows interactive calculations.
First, let's create a new C file for our circumference calculation:
cd ~/project
nano circle_circumference.c
Now, let's write the code to read the radius:
#include <stdio.h>
int main() {
float radius;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
printf("Radius entered: %.2f\n", radius);
return 0;
}
Let's break down the code:
float radius;declares a floating-point variable to store the circle's radiusprintf()displays a prompt to the userscanf()reads the floating-point number entered by the userprintf()confirms the radius entered by printing it with two decimal places
Compile and run the program:
gcc circle_circumference.c -o circle_circumference
./circle_circumference
Example output:
Enter the radius of the circle: 5.5
Radius entered: 5.50
Compute Circumference = 2 _ PI _ r
In this step, we will modify our previous program to compute the circumference of a circle using the mathematical formula: Circumference = 2 _ π _ radius.
Open the existing file and update the code:
nano ~/project/circle_circumference.c
Replace the previous code with the following:
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, circumference;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
circumference = 2 * PI * radius;
printf("Radius: %.2f\n", radius);
printf("Circumference: %.2f\n", circumference);
return 0;
}
Key changes in the code:
- Added
#define PI 3.14159to define a constant for π - Introduced
circumferencevariable to store the calculation - Computed circumference using the formula: 2 _ π _ radius
- Added an additional
printf()to display the calculated circumference
Compile and run the program:
gcc circle_circumference.c -o circle_circumference
./circle_circumference
Example output:
Enter the radius of the circle: 5.5
Radius: 5.50
Circumference: 34.56
Print the Circumference
In this final step, we will format and print the circumference with clear, user-friendly output. We'll enhance the previous program to provide a more professional presentation of the results.
Open the existing file for final modifications:
nano ~/project/circle_circumference.c
Update the code with improved formatting:
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, circumference;
printf("Circle Circumference Calculator\n");
printf("-------------------------------\n");
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
circumference = 2 * PI * radius;
printf("\nCalculation Results:\n");
printf("Radius: %.2f units\n", radius);
printf("Circumference: %.2f units\n", circumference);
return 0;
}
Key improvements in the code:
- Added a title and separator for better user experience
- Included descriptive labels for radius and circumference
- Added units to make the output more informative
- Used
\nfor better spacing and readability
Compile and run the program:
gcc circle_circumference.c -o circle_circumference
./circle_circumference
Example output:
Circle Circumference Calculator
-------------------------------
Enter the radius of the circle: 5.5
Calculation Results:
Radius: 5.50 units
Circumference: 34.56 units
Summary
In this lab, we learned how to read the radius of a circle from user input using the scanf() function, and then compute the circumference of the circle using the formula Circumference = 2 * π * radius. We defined the value of π as a constant using #define PI 3.14159, and then performed the calculation to obtain the circumference. Finally, we printed the radius and the computed circumference to the console.



