# C language to find the volume and surface area of the ball, the volume is equal to the surface area?

2022-02-04 16:42:11

Error code :
#include<stdio.h>
#include<math.h>
int main()
{
const double PI = 3.14159;
double r, volume, surface_area;
scanf("%lf", &r);
volume = 4PIpow(r,3)/3;
surface_area = 4PIpow(r,2);
printf("volume=%lf, surface_area=%lf", volume, surface_area);
}

Corrected :
#include<stdio.h>
#include<math.h>
int main()
{
const double PI = 3.14159;
double r, volume, surface_area;
scanf("%lf", &r);
volume = 4/3PIpow(r,3);
surface_area = 4PIpow(r,2);
printf("volume=%lf, surface_area=%lf", volume, surface_area);
}

I don't understand ！！ Why? , Formula for volume /3 To put 4 The back is right ？ and , Even if the volume formula is wrong , Why is the output equal to the area ？

Did you post it upside down
If integer division .4/3=1, If one side of the division operation is a floating point number , As a floating point number , therefore /3 The position of the affects the result

volume = 4.0/3*PI*pow(r,3);

No problem , Revised as follows , For reference ：

``#include<stdio.h>#include<math.h>int main(){    const double PI = 3.14159;    double r, volume, surface_area;    printf("Please input the radius of the ball: ");    scanf("%lf", &r);    volume = 4.0 * PI * pow(r, 3) / 3;    //volume = 4.0 / 3 * PI * pow(r, 3);    surface_area = 4.0 * PI * pow(r, 2);    printf("volume=%lf, surface_area=%lf", volume, surface_area);    return 0;}``