November 14, 2021, Learn eTutorial

883

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

**Radius r:**Radius is the length of the line that joins any point on the circle and its center.**Diameter d:**The diameter of a circle can be defined as the double of the radius, that is a line that connects two points on the circle which passes through the center.

The area of a circle is calculated using the formula, ** Π r ^{2}**, where,

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 **Π r ^{2}**

- "
**Π * 2 * 2**" - that is "
**Π* 4**" - The value of
**Π**is 3.14 - then the final area will be 3.14 * 4 = 12.56.

- Let's check how we implement the above logic in the C program.
- We are defining the value of '
**pi**' as**3.14**as it is a constant value. - To apply the formula that use to find the ara of the circle we are using
`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