#include <stdio.h>
#define PI 3.14
/* C program to Input integer radius and find area of Circle. */
int main()
{
int r;
float areaCircle=0.0;
/* Take input radius from user using scanf function */
printf("Enter a radius for Circle.\n");
scanf("%d", &r);
/* calculate Area of Circle */
areaCircle=PI*r*r;
printf("\nRadius of Circle = %d", r);
printf("\nValue of PI= %.2f",PI);
printf("\n Area of Circle=%.2f",areaCircle);
return 0;
}
The program outputs these results:
Enter radius of circle:
2
Radius of circle = 2
Value of PI = 3.14
Area of circle = 12.56
Explanation:
Well, here's how the program goes about calculating and displaying the area of a circle:
First, we declare PI:
#define PI 3.14
So we set-up PI first with a value that we would later use to calculate the area of the circle. Here we have taken
3.14, which is agreeable for most calculations.
Next, we set up storage:
int r;
float areaCircle = 0.0;
We prepare two little "storage boxes" to hold some numbers:
r: This box will hold the radius of the circle (that's the distance from the center to the edge). The user will type this in. We use int because the radius is a whole number.
areaCircle: This box will hold the calculated area of the circle. We use float because the area might have a decimal part (a fraction). It starts at 0.0.
We ask the user for the radius:
printf("Enter the radius of the circle: \n");
scanf("%d", &r);
The program, therefore, asks you to type in the radius of the circle.
The scanf function then takes the number you type and stores it in the r storage box.
And now, for the area computation:
areaCircle = PI * r * r;
The program computes the area of the circle, using the formula:
Area = PI * radius * radius.
If you enter a radius of 2 (r = 2),
then the program calculates the area to be 3.14 * 2 * 2 = 12.56.
Finally, the results are presented to you:
printf("\nRadius of the circle = %d", r);
printf("\nValue of PI = %.2f", PI);
printf("\nArea of the circle = %.2f", areaCircle);
The program outputs:
The radius entered into the program.
The value of PI used (3.14).
The calculated area of the circle.