C Programming | To calculate the area of a Circle

Write a C Program to calculates the area (floating-point number with two decimal places) of a circle given its the radius (integer value). The value of Pi is 3.14.

Video Tutorial :






In this program, we will find out the area of a circle after the user inputs value of the radius of the circle.

At the start, we will define the value of pi as 3.14 for ease of further calculation into our given program here.

#define PI 3.14

Then after that, we will assign the variable 'radius' as an integer'. This variable will store the input radius integer value at the start of the program execution, in this case, we are using integer data type but you can also use float data type if you want to.

int radius;

Now, we also define another variable in our code, named 'area' of float datatype. This variable will store the calculated area value after our code finishes the process of calculating the area of the circle from radius given and the other formula.

float area;

In the next step out code will ask the user to input the value of the radius when the program is executed properly after compiling, we are using the 'scanf' command here for this task of storing the data of radius into our code.

scanf("%d", &radius);

Now, in this step we will finally use the formula to find the area of a circle when the radius is given, i.e. (𝜋 r²) where 𝜋 = 3.14 and r = radius of the circle. In our program, we will be storing the final value of the area in the variable named 'area' so we will equate 'area' variable with the formula "PI*radius*radius"

area=PI*radius*radius;

This will give us the desired output, i.e. the area of the circle. For example, let us assume the user input the radius of the circle as '5'. Then the program will calculate it as "5x3.14x3.14" which is equal to '78.50'. 

Finally, we will use the print statement. So the user will be shown the output as "Area of a circle = 78.50".

printf("Area of a circle = %5.2f\n", area);

Note: 
  • The "%5.2f" ‘5’ represents the number of spaces to be printed before the float value in the output. This helps in maintaining the right alignment of values.
  • ‘2’ represents the precision of float value. It will only take two digits after the decimal point regardless of input.
  • ‘f’ is used to represent float value.

C Program:





Post a Comment

0 Comments