In this post, we will learn how to write a program to calculate the area of a circle in the C Programming language.
A circle is a shape consisting of all points in a plane that are at a given distance from a given point, the center.
The area of a circle is pi times the radius squared (A = π r²), where r is the radius of the circle and π = 3.14(approx).
We will be using the above formula in our program to calculate the area of a circle.
So, without further ado, let’s begin this tutorial.
C Program To Calculate Area of a Circle
// C Program To Calculate Area of a Circle #include <stdio.h> int main(){ float radius, area; // Asking for input printf("Enter the radius of the circle: "); scanf("%f", &radius); // Calculating area area = 3.14 * radius * radius; printf("Area of the circle is: %.2f", area); return 0; }
Output
Enter the radius of the circle: 7
Area of the circle is: 153.86
How Does This Program Work ?
float radius, area;
In this program, we have declared two float data type variables named radius and area.
// Asking for input printf("Enter the radius of the circle: "); scanf("%f", &radius);
Then, the user is asked to enter the radius of the circle. The value of radius will get stored in the radius named variable.
// Calculating area area = 3.14 * radius * radius;
Then, we calculate the area of the circle using the formula π r². The value of π is approximately 3.14.
printf("Area of the circle is: %.2f", area);
Finally, the area of the circle is printed using printf() function. Here we have used %.2f format specifier because we want to display the result only till 2 decimal places.
Conclusion
I hope after going through this post, you understand how to write a program to calculate the area of a circle in C.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.
Also Read: