Simple C program that calculates and identifies the area of a circle when you provide the radius as input:
#include <stdio.h>
int main() {
// Declare variables
double radius, area;
// Ask the user to enter the radius
printf("Enter the radius of the circle: ");
scanf("%lf", &radius);
// Calculate the area of the circle using the formula: π * r * r
area = 3.14159265359 * radius * radius;
// Display the result
printf("The area of the circle with radius %.2lf is %.2lf square units.\n", radius, area);
return 0;
}
Here’s a step-by-step explanation of the program:
- We include the
stdio.h
header for input and output functions. - We declare two variables:
radius
(to store the radius of the circle) andarea
(to store the calculated area). - We use
printf
to ask the user to enter the radius of the circle andscanf
to read the input and store it in theradius
variable. - We calculate the area of the circle using the formula
π * r * r
, whereπ
is approximately 3.14159265359. - Finally, we use
printf
to display the result, including the radius and the calculated area, with two decimal places.
To run this program, follow these steps:
- Open a C compiler (like GCC) on your computer.
- Copy and paste the program into a new C file (e.g.,
circle_area.c
). - Compile the program by running
gcc circle_area.c -o circle_area
in the command prompt or terminal. - Run the program by typing
./circle_area
and providing the radius when prompted.
The program will then calculate and display the area of the circle based on the radius you enter.