My code shows me a wrong output by showing the area to be zero:
#include <stdio.h>
double calc(float rad);
void main(void) {
float rad;
printf("Enter the circle radius: \n");
scanf("%f",&rad);
printf("You entered: %f\n",rad);
printf("Area is %d\n",calc(rad));
if(calc(rad)>1000){
printf("Area is > 1000");
}
else{
printf("Area is < 1000");
}
}
double calc(float rad){
double area=3.14*rad*rad;
return area;
}
Output:
Enter the circle radius : 20
you entered 20.000000
area is 0
area > 1000
Desired output:
Enter the circle radius : 20
you entered 20.000000
area is 1256.000000
area > 1000
>Solution :
The erroneous output is as a result of using %d instead of %lf for double or %f for float in your printf statement. Turning on compiler warning -Wformat will highlight this during compilation