Daniel Toh - 18 days ago 5

C Question

I'm learning C currently, and when I was making a program to calculate the area of a circle, I assigned "3.1415926535" to a constant float Pi. When I used it in my program as areaCircle = Pi * radius * radius, where radius was an integer and areaCircle was a float, the program printed out something like "7.000000".

EDIT: This is the code, if you guys need it:

`int length,breadth,radius,perimeter,areaRectangle;`

float circumference,areaCircle;

const int PI = 3.14159265358979323846264338327;

printf("Enter the length and breadth of the rectangle: \n");

scanf("%d %d",&length,&breadth);

perimeter = 2*(length+breadth);

areaRectangle = length*breadth;

printf("The perimeter of the rectangle with length %d and breadth %d\nwill be %d and its area will be %d \n",length,breadth,perimeter,areaRectangle);

printf("Enter the radius of the circle: \n");

scanf("%d",&radius);

circumference = PI*2*radius;

areaCircle = PI*radius*radius;

printf("The circumference of a circle with radius %d is %f and its area is %f\n",radius,circumference,areaCircle);

Answer

In the code you posted you have declared `PI`

as an `int`

. Try:

```
const float PI = 3.14159265358979323846264338327;
```

Source (Stackoverflow)

Comments