C Language | Area of a Circle

Sunday, 9 December 2012

In Geometry, there are a lot of shapes that has different sides and sizes but there is one shape that has no sides and that is circle. A circle has an infinity line because of its round shape. The area of a circle is the simple thing you can calculate because of its simple equation. You only need to multiply the radius and pi then multiply again the result to the radius: a = ((PI * r) * r), respectively.

To calculate the area of a circle easily I made a program out of its equation. Since we're going to use PI anywhere in the program we need to declare it globally, meaning we need to declare it out of the main program. Check out the sample code below.


    1: #include <stdio.h>
   2:  #include <conio.h>
   3:  #define PI 3.141
   4:   
   5:  int main()
   6:  {
   7:    float r, a;
   8:   
   9:    printf("Radius: ");
  10:    scanf("%f", &r);
  11:   
  12:    a = ((PI * r) * r);
  13:   
  14:    printf("%f\n", a);
  15:    getch();
  16:  }


The program operates like this. As seen above, the PI is declared as a global variable because we are going to use it anywhere in the program and to minimize declaring it again and again we declared it as a global variable. In the main program, we have declared two variables under the float data type identifier. When run, the program will ask the user to enter the radius of a circle then it will go through the equation where it will calculate the area. After that, it will then show you the result. Then you can terminate the program. See the sample output below.


Hope you find this post helpful. Feel free to leave a comment below. Thanks!


No comments:

Post a Comment

Please do leave a comment. It's gonna be a friendly conversation between programmers. Hope you do find our blog helpful. Thank you!

Related Posts Plugin for WordPress, Blogger...