Example of C program for area of circle using radius or diameter of a circle. Program will use area of circle formula i.e. PI*r*r, where r is the radius of the circle and value of PI = 22/7 = 3.142 to calculate area of the circle.
On the top, the value of PI will be defined in the program as #define PI 3.142 . A function area(int radius) will be introduced to calculate area of the circle and return the result.
This program example, accept radius from the user as a float value. If you want to receive diameter instead of radius then first, divide the diameter by 2 to get radius and pass to area function.
C program for area of circle
/*------------------------------------
* C program to calculate area of a Circle
*/
// Define macro PI that will be used
//to calculate area of circle
#define PI 3.142
/* Area function that receives Radius of circle
* calculate area and return the result
* return the result
*/
float area(float r){
return PI*r*r;
}
//Test program
int main()
{
float Radius =0;
float result =0;
printf("Enter Radius of the Circle:");
scanf("%f",&Radius);
//call area function and receive the
//area
result = area(Radius);
printf("Area of Circle = %f ", result);
return 0;
}
Output:
Enter Radius of the Circle:4.0
Area of Circle = 50.271999