Calculate Area of a Circle in C
Calculate the Area of a Circle in C
This C program calculates the area of a circle based on the radius provided by the user. Below is the code for the program:
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, area;
// Input the radius of the circle
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
// Calculate the area of the circle
area = PI * radius * radius;
// Display the result
printf("The area of the circle with radius %.2f is %.2f\\n", radius, area);
return 0;
}
Explanation
The program starts by including the <stdio.h>
header file to utilize input and output functions.
A constant PI
is defined with the value 3.14159 for Pi.
In the main
function, two floating-point variables, radius
and area
, are declared.
The program prompts the user to enter the radius of the circle and reads this value using scanf
.
The area is then calculated using the formula area = PI * radius * radius
, and the result is displayed using printf
.
Comments
Post a Comment