Reputation:
#include <stdio.h>
void volume()
{
float pi=3.14,r,v;
printf("\nEnter the radius: ");
scanf("%f",&r);
v=(1/3)*pi*r*r*r;
printf("%f",v);
}
int main()
{
volume();
}
This is C program to find volume using functions. Compiler shows no error or warnings but the output is always zero.
Upvotes: 0
Views: 817
Reputation: 881323
It's due to the fact that 1 / 3
is done as integer division because both operands are type int
(a). So the final result is zero.
You can get the intended effect simply by ensuring one of the operands is non-integer, such as with:
v = (1.0f / 3) * pi * r * r * r;
That will work, because it results in a float (inside the parentheses) of 0.333...
. However, there's absolutely no real reason why you need to parrot the equation shown in text books, you can achieve the same result with the simpler:
v = pi * r * r * r / 3;
Because all those variable are floating point, the result of the final something / 3
is also done as floating point.
And, just some quick advice, you may want to consider using double
types rather than float
. The float
type generally uses less space but, unless you have large arrays of them, it's not usually a problem. The double
type gives a much greater range and precision.
In addition, 3.14
is not really that precise a value for pi
. Most implementations will define an M_PI
constant in math.h
for you to use but it's not mandated by the standard. So, you can use something like this to get a more accurate value:
#include <stdio.h>
#include <math.h>
// Define if implementation doesn't provide.
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
static void GetRadiusAndPrintVolume(void) {
printf("\nEnter the radius: ");
double radius;
scanf("%lf", &radius);
double volume = M_PI * radius * radius * radius / 3;
printf("Volume for radius %f is %f\n", radius, volume);
}
int main() {
GetRadiusAndPrintVolume();
}
And, finally, you may want to check that equation of yours. Though you don't say explicitly, it very much looks like it's supposed to be the volume of a sphere.
If that is the case, the formula should be 4/3 π r3
rather than 1/3
. Hence the statement would be:
double volume = M_PI * radius * radius * radius * 4 / 3;
(a) If you're interested, this is all covered by the "Usual arithmetic conversions" section of the C standard (section 6.3.1.8
in C11
):
Many operators that expect operands of arithmetic type cause conversions and yield result types in a similar way. The purpose is to determine a common real type for the operands and result.
It then goes on to list what happens for specific cases, which is generally that the "lesser" (in terms of its range and/or precision) operand is upgraded to the same type as the "greater" operand. That's the basic idea though, if you want the full picture, you should refer to the previously mentioned section.
In your specific case, since both operands of 1 / 3
are of type int
, the calculation and the result is done as an int
, meaning it truncates the fractional part (giving zero).
Upvotes: 1
Reputation: 2105
Because 1/3 is not 0.3333 in C, it is 0.
Solution:
#include <stdio.h>
void volume()
{
float pi=3.14,r,v;
printf("\nEnter the radius: ");
scanf("%f",&r);
v=(((float)1)/3)*pi*r*r*r;
printf("%f",v);
}
int main()
{
volume();
}
Upvotes: -1
Reputation: 85767
That's because anything multiplied by 0
is 0
, and 1/3
is 0
.
1/3
is 0
because both 1
and 3
are literals of type int
, and dividing two integers results in an integer again.
Fix: 1.0/3.0
because 1.0
and 3.0
are floating point literals, so you get floating-point division.
Upvotes: 1
Reputation: 625
you have zero here due to 1/3
is integer division = 0
replace v=(1/3)*pi*r*r*r;
with v=(1.f/3.f)*pi*r*r*r;
Upvotes: 4