Anyone knows why it din work

The volume shown is always 0, dun know why.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  #include <stdio.h>
#define PIE 3.14
float cone (float height,float radius);
int main ()
{
	float height,radius;
	printf("Enter your cone height:");
	scanf("%f",&height);
	printf("Enter your cone radius:");
	scanf("%f",&radius);
	printf("The volume of your cone is %f",(cone(height,radius)));
}
float cone (float height,float radius)
{
	float volume;
	volume=(1/3)*PIE *radius*radius*height;
	return volume;
}
integer division returns an integer
1/3 is 0, and if you multiply by 0 the result is 0

write 1.0/3.0 instead.
Here, again i don't know C that well, tough if your math is correct this should do the job
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#define PIE 3.14
float cone (float height,float radius);
int main ()
{
	float height,radius;
	printf("Enter your cone height:");
	scanf("%f",&height);
	printf("Enter your cone radius:");
	scanf("%f",&radius);
	printf("The volume of your cone is %f",(cone(height,radius)));
}
float cone (float height,float radius)
{
	float volume;
	volume=PIE*radius*radius*height/3;
	return volume;
}
Haha ne555 is pro. He is damn right. 1/3 is 0 because of the integer division. I miss that part. Thanks bro. you are smart.
Also,
(1/3) * pie *radius *radius*height =
(1 * pie *radius * radius * height) /3 =
pie * radius * radius * height /3.
ALWAYS do the multiplications before the divisions, it leads to better accuarcy.
If the concern is about accuracy, the place to start is here:
#define PIE 3.14

Also, use type double instead of float.
Last edited on
Topic archived. No new replies allowed.