0

I'm trying to calculate the area of a circle in C as an assignment, I've written some code to help me do this, but it's not giving the expected results, It is supposed to give the following results in order for it to get approved:

This is what the result should be:

Input Result 5.0 Please enter the radius: The area of the circle is 78.539750

This is the result I am getting:

Please enter the radius: The area of the circle is 78.537498

Has anyone got any ideas? They would be greatly appreciated! :)

#include <stdio.h>
#include <math.h>


int main(void)
{
  double PI = 3.14159;

  /* declare the other required variables */
  float area, radius;


  
  /* get the input from the console */
  printf("Please enter the radius:\n");
  scanf("%f",&radius);
 

  /* compute the area */
  area = PI * radius * radius;


  /* write out the answer */
  printf("The area of the circle is %f\n", area);
  return 0;
}
  • You might want to use `double` instead of `float`. Remember to change the `scanf` format for that though. – Some programmer dude Oct 17 '21 at 17:09
  • Your PI is not very precise, maybe use `M_PI` from `math.h`. – Cheatah Oct 17 '21 at 17:10
  • @Cheatah Note that there's no `M_PI` specified in standard C. It's added by e.g. POSIX though, and exists in the MSVC `math.h` header as well. – Some programmer dude Oct 17 '21 at 17:14
  • A `#define PI 3.14159265358979323846` or `#define PI 4. * atan(1)` (value is the same, the first avoids a header dependency and eliminates the computation -- but a good compiler should make that optimization for you) – David C. Rankin Oct 17 '21 at 17:27

0 Answers0