current position:Home>C language to find the volume and surface area of the ball, the volume is equal to the surface area?

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 CSDN Q & A

Error code :
#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 = 4PIpow(r,3)/3;
surface_area = 4PIpow(r,2);
printf("volume=%lf, surface_area=%lf", volume, surface_area);
}

img

Corrected :
#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/3PIpow(r,3);
surface_area = 4PIpow(r,2);
printf("volume=%lf, surface_area=%lf", volume, surface_area);
}

img

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 ?




Take the answer :

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



Other answers 2:

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


Other answers 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;}

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/02/202202041642091904.html

Random recommended