C Program To Calculate The Area of A Circle

Circle area calculation is a common problem for programming. By writing this code, a beginner can learn how to take user input, how to calculate something, how to show output. In this article, we will show you the code and also going to explain line-by-line.

C program for circle area

Main Theory

Circle Area = `\pi r^2` [Here, r is the radius of the circle]

To calculate the area of the circle we have to know the radius of the circle. We can take that from the user then have to square the value of the radius. Then we have to multiply that with Pi. Finally, show the result as output.

Code

#include <stdio.h>
#define PI 3.1416

int main()
{
  float radius;
  float area;
  
  printf("Enter the radius of a circle\n");
  scanf("%f", &radius);
  
  area = PI*radius*radius;
  
  printf("Area of the circle = %.2f\n", area);

  return 0;
}


Output:

Enter the radius of a circle
25
Area of the circle = 1963.50

Process returned 0 (0x0)   execution time : 3.627 s
Press any key to continue.

Code Explanation

At first, we declare the header file then we defined the value of Pi. We defined PI using #define PI 3.1416. It means PI is equaled to 3.1416 or we can say `\pi=3.1416`.

Then in the main function, we declare two float type variables. Using scanf() we take the radius value from the user and then we store it inside the radius variable. Then we use the equation and store the result inside the area variable. Here is the simplified version of that formula:

Circle area = `\pi r^2` = `\pi\times r\times r`

Print the area after calculating the area and storing the result inside the area variable. Using printf() you can print the result and show it to the user. That's how this program works.

Read more: C Program To Reverse A Number

Conclusion

This is a beginner-friendly program. If you understand the full concept of this program then you can actually write another program like this. You just need to understand the formula and according to the formula, you can easily write the code. Always try to understand the code never try to memories it. Read our other articles about c programming and subscribe to our email subscription.

Post a Comment (0)
Previous Post Next Post