# Program compiles but don't think the math formulas are calculating correctly.

• 10-07-2013, 10:26 PM
MichiKen
Program compiles but don't think the math formulas are calculating correctly.
Trying to get familiar with using math functions in Java. Trying to run a program that calculates the volume of a cone. The program compiles but I get a different value when I manually enter the numbers into a calculator compared to the value that displays when the program runs in Java.

The formula for the volume of a cone is: pi * height * radius * radius /3

When entering the values into a calculator I get 261666.66

When compiling and running the program in java, it returns the value of 261799.38

Here's the code:

Code:

``` //Purpose of this program is to calculate the volume of a cone. import java.lang.Math; import java.util.*; class Cone {   double r; //declare radius variable   double h; //declare height variable   public void setR(double radius)   {         r = radius;   }   public void setH(double height)          {         h = height;   }    public double getR()   {         return r;   }   public double getH()   {         return h;   }   public double VolumeFormula()   {         double volume = Math.PI * h * Math.pow(r,2)/3;         return volume;   } }         class ConeTestDrive        {                 public static void main (String[] args)        {                         Cone c = new Cone();                         c.setR(50);                         c.setH(100);                         System.out.println("Radius:" + c.getR());                         System.out.println("Height:" + c.getH());                         System.out.println("The volume of the cone is:"                                        + c.VolumeFormula());                 }         }```
• 10-07-2013, 11:56 PM
SurfMan
Re: Program compiles but don't think the math formulas are calculating correctly.
You must have entered the formula wrong in the calculator. I get these results:
Calculator: 3.141592653589793 * 100 * 50 * 50 / 3 = 261799.38779914941666666666666667
Your program: The volume of the cone is: 261799.38779914947
• 10-08-2013, 12:06 AM
MichiKen
Re: Program compiles but don't think the math formulas are calculating correctly.
Ah. I only entered 3.14 for pi when I entered it into the calculator. Didn't realize there was that much of a difference. Thanks.