For a better understanding of this C program example, we always recommend you to learn the basic topics of C programming listed below:
In this C program, we need to find the area of a circle with the radius given. A circle is a shape that has no corners, and the area of the circle is defined as the region that is inside that closed shape.
In a circle, we have two important measurements which are
The area of a circle is calculated using the formula, Π r2, where, r is the radius.
Let's take an example of a circle with a diameter of 4. which means the circle has a 4 cm length between the longest points. then its radius will be 2.
Now we can calculate the area by using the formula Π r2
pow function
which is defined in the math
library for calculating the square of the radius, and then we are using precision to the float value to print only two decimals after the dot.STEP 1: Import the header file libraries into the C program to use built-in functions such as stdio
, conio
, and math.h
.
STEP 2: Define the value of 'pi' as '3.14' using C programming syntax.
STEP 3: Open the main() to start the program in the C language.
STEP 4: After initializing the variables for radius and area using float
data type, accept the value of radius from the user using printf
and scanf
functions in C.
STEP 5: Calculate the area of the circle using the formula 'pi * pow (radius,2)'.
STEP 6: Print the result using printf
statement, Here we use 5.2f which adds Precision in the float data type. which means it prints only that much decimal value as result.
#include <stdio.h>
#include <math.h>
#define PI 3.142
void main()
{
float radius, area;
printf("Enter the radius of a circle\n");
scanf ("%f", & radius);
area = PI * pow (radius,2);
printf ("Area of a circle = %5.2f\n", area);
}
RUN 1 ======= Enter the radius of a circle 3.2 Area of a circle = 32.17 RUN 2 ======== Enter the radius of a circle 6 Area of a circle = 113.11