Program to Calculate the Area of a Circle in C
Calculating the Area of a Circle
The area of a circle is calculated using the formula:
Area = π × r², where 'r' is the radius of the circle.
We will explore three different methods to calculate the area of a circle in C programming.
Method 1: Using Basic Arithmetic
This method directly computes the area using the formula.
#include <stdio.h>
#define PI 3.14159
int main() {
float radius, area;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("Area of the circle: %.2f\n", area);
return 0;
}
Output: Area of the circle: 78.54
Method 2: Using a Function
This method uses a function to calculate and return the area.
#include <stdio.h>
#define PI 3.14159
float calculate_area(float radius) {
return PI * radius * radius;
}
int main() {
float radius;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
printf("Area of the circle: %.2f\n", calculate_area(radius));
return 0;
}
Output: Area of the circle: 153.94
Method 3: Using Recursion
This method demonstrates recursion, though recursion is not typically needed for simple mathematical operations.
#include <stdio.h>
#define PI 3.14159
float recursive_area(float radius, int times) {
if (times == 0) return PI * radius * radius;
return recursive_area(radius, times - 1);
}
int main() {
float radius;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
printf("Area of the circle: %.2f\n", recursive_area(radius, 1));
return 0;
}
Output: Area of the circle: 50.27