# Thread: Help With Calculation

1. ## Help With Calculation

This is written in C. The program works, but the calculation is wrong, can someone tell me what I am doing wrong?

Thank You

Code:
#include "stdafx.h"
#include <math.h>

#using <mscorlib.dll>

using namespace System;

// Function prorotypes

void sphere_volume (double);

// PI is a constant

const double PI=3.14159;

int _tmain()

{
// Integer local to Main to store initial radius value
double r;

printf("Please Enter A Positive Radius Value\n");
scanf("%lf", &r);

// If Else Function To Make Sure The User Types A Positive and Not A Negative Integer.

if (r >= 0)
sphere_volume (r);

else
printf("You Must Enter A Positive Integer!!!! Not A Negative Integer!!!\n");

return 0;

}

// This Is The Fnction That Does The Calculations For The Volume.

void sphere_volume (double radius)

{
double  radius_cubed;

double exponent=3;

double volume;

printf("This Is The Test To See If R Was Passed To The Function: %lf\n", radius);

radius_cubed=pow(radius, exponent);

volume=(4/3)*PI*radius_cubed;

printf("The Volume Of The Sphere is %lf\n", volume);
}

2. There is a C programming forum for C programs.

Your problem is this:
Code:
(4/3)
make it
Code:
(4.0/3)

3. The reason is: 4/3 is integer division. The fractional part is discarded. So it ends up being 1, not 1.33333333333333. 4.0 is a floating point number, and the result of that expression is the expected value, 1.3333333333333333.

Oh yes, and
Code:
printf("The Volume Of The Sphere is %lf\n", volume);
Should be
Code:
printf("The Volume Of The Sphere is %f\n", volume);
With printf(), %lf doesn't exist.

4. Thank you, I am trying it now. Were is the C programming forum for C programs.

5. Sorry I found it.

Thank You

Popular pages Recent additions