C Program to calculate the area of circle
Objective
Write a C program to calculate the area of a circle.
Algorithm / Approach
- Declare a float variable
radiusandarea. - Declare a constant float
pi = 3.14f. - Read the radius from the user using
scanf("%f", &radius). - Calculate the area using the mathematical formula:
area = pi * radius * radius. - Print the result using
printf("%f", area).
main.c
#include<stdio.h>
int main( )
{
float radius, area;
const float pi = 3.14f;
printf("Enter radius: ");
scanf("%f",&radius);
area = pi*radius*radius;
printf("CIRCLE DETAILS:\n");
printf("RADIUS:%f\n",radius);
printf("AREA:%f\n",area);
return 0;
}
Expected Output
Enter radius : 8.2 CIRCLE DETAILS: RADIUS:8.2 AREA :211.1336
Explanation of the Program
- Because the radius and area can have decimal points, we use the
floatdata type instead ofint. - The
constkeyword is used to declarepi. This ensures that the value of pi remains constant throughout the program and cannot be accidentally modified. - The format specifier for floats in
printfandscanfis%f.
Complexity
Time Complexity
O(1)
Space Complexity
O(1)