C Program to Find Area and Perimeter of Circle
In this article, we will learn how to find a circle's area and perimeter using a C program.
Circle is a geometrical shape that we have all known since childhood. It is a closed area where the boundary is equidistant from a common point.
In this figure, the boundary of the closed figure is at the same distance from the common point. The radius of a circle is defined as the length from the common point (centre) to the circle's boundary.
A circle has two main properties, i.e., Perimeter and Area.
Perimeter of Circle
Perimeter is the total distance covered around the boundary of the closed figure. In the case of a circle, the perimeter is the total distance covered by any object if the object starts walking from a point on the boundary of a circle and move along the boundary of the circle till it reaches the same point. The perimeter of a circle is an approximate value greater than six times the circle's radius. So, to get a numerical value, we use the mathematical constant π to calculate the perimeter of the circle and the radius of the circle.
The formula for the perimeter of a circle is
Perimeter of Circle = 2 * π * r
Where π is the mathematical constant with an approximate value of 3.14159, and r is the circle's radius.
Area of Circle
An area is a region that is bounded by a closed figure. Similarly, the Area of the circle is the total region enclosed by the circle's boundary. Let’s practically understand how to find the area of a circle.
We have a circle with an r radius. Create more concentric circles with a radius less than r within the given circle. Now, cut out the circle from a fixed point to the centre and along the different circles drawn with different radii. It will form a rectangular strip of different lengths. The outer rectangular strip has a length of the circle's perimeter, and all the other rectangular strips are smaller. Placing all the strips in order, will make approximately a triangle whose base is equal to the circle's perimeter and height is equal to the circle's radius. After calculating the area using the formula of a triangle, we get
Area = 1/2 * perimeter of circle * radius of circle = 1/2 * (2 * π * r) * r = π * r * r
So, the Formula of the area of a circle is
Area of Circle = π * r * r
Here π is the mathematical constant with an approximate value of 3.14159, and r is the circle's radius.
We know about the Circle and its area and perimeter. Let’s move forward and learn how to code this problem in C.
C Program
Without Function:
#include <stdio.h>
int main() {
float r, Area, Perimeter;
const float PI = 3.14159;
printf("Enter the radius of the circle: ");
scanf("%f", &r);
Area = PI * r * r;
Perimeter = 2 * PI * r;
printf("Area of the circle = %f\n", Area);
printf("Perimeter of the circle = %f\n", Perimeter);
return 0;
}
Output:
Enter the radius of the circle: 5
Area of the circle = 78.539749
Perimeter of the circle = 31.415901
In the above C program, we define three variables, r, Area, and Perimeter, to store the value of the radius, Area, and Perimeter of the circle. Then, with the help of the const keyword, we declare a constant value PI equal to 3.14159. This constant is used by the formula to calculate Area and Perimeter. In the next step, the user is prompted to enter the radius value, which will be stored in the r variable.
Next, we use the circle's area and perimeter formula, and the result is stored in Area and Perimeter variables, respectively. Finally, the value of the area and perimeter of the circle is printed onto the console.
With Function:
#include <stdio.h>
#define PI 3.14159
float Area(float x){
return PI * x * x;
}
float Perimeter(float x){
return 2 * PI * x;
}
int main() {
float r;
printf("Enter the radius of the circle: ");
scanf("%f", &r);
printf("Area of the circle = %f\n", Area(r));
printf("Perimeter of the circle = %f\n", Perimeter(r));
return 0;
}
Output:
Enter the radius of the circle: 10
Area of the circle = 314.158997
Perimeter of the circle = 62.831799
In the above code, we define a constant using the #define pre-processor. We define two functions, ‘Area’ and ‘Perimeter’, which take the value of Radius as a parameter and return the area and perimeter of the circle according to the formula.
The user is prompted to enter the circle's radius value and store it in the r variable in the main function. We call the Area and Perimeter functions using this radius value as a parameter and print the result to the console.
Conclusion
In this article, we cover a basic topic related to geometrical shapes. We learn the C program to find the Area and Perimeter of the circle using function and without using function.