# Haskell Program to calculate the volume and area of Cone

This tutorial will help us in calculating the volume and area of the Cone. The volume of a cone is a measure of the amount of space inside the cone. And area involves the surface area of the cone that is obtained from the lateral area of a cone. The formula for the volume of a cone is V = (1/3) * π * r^2 * h, where r is the radius of the base of the cone, h is the height of the cone, and π is approximately equal to 3.14.

And the formula for the surface area of a cone is A = π * r * s, where r is the radius of the base of the cone and s is the slant height of the cone.

## Algorithm

• Step 1 − The variables named, “radius” and “height” is being initialized. It will contain the radius and height of the cone whose surface area and volume is to be computed.

• Step 2 − The volume and area functions are defined on the basis of simple mathematical formula as volume = (pi * radius^2 * height) / 3 ; lateralArea = pi * radius * sqrt(radius^2 + height^2) and surfaceArea = lateralArea + pi * radius^2 respectively.

• Step 3 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. The main function takes input from the user for the radius and height of the cone and then uses the above functions to calculate the volume and surface area

• Step 4 − The final resultant volume and surface area value is displayed by using ‘putStrLn’ statement and show function once the volume and area functions are called.

## Using User-defined Function

In this example, we use the value of pi and the square root function. The radius and height of the cone are defined as variables at the beginning of the program. We then use these variables to calculate the volume, lateral surface area, and total surface area of the cone using the appropriate formulas. Finally, we use the putStrLn function to print the results to the console.

### Example

radius = 5
height = 10
volume = (pi * radius^2 * height) / 3
surfaceArea = lateralArea + pi * radius^2
main = do
putStrLn ("Volume: " ++ show volume)
putStrLn ("Surface Area: " ++ show surfaceArea)


### Output

Volume: 261.79938779914943
Surface Area: 254.160184615763


## Using Cone Data Type

This approach defines a Cone data type, which is a record with two fields: radius and height. We then define three functions coneVolume, coneLateralArea and coneSurfaceArea that take a Cone as a parameter and return the respective volume and area. In main function, we create a Cone with radius and height value and then use the defined functions to calculate the volume and surface area and print it to the console.

### Example

data Cone = Cone { radius :: Double, height :: Double }

coneVolume :: Cone -> Double
coneVolume (Cone r h) = (pi * r^2 * h) / 3

coneLateralArea :: Cone -> Double
coneLateralArea (Cone r h) = pi * r * sqrt (r^2 + h^2)

coneSurfaceArea :: Cone -> Double
coneSurfaceArea c = coneLateralArea c + pi * (radius c)^2

cone = Cone 5 10
main = do
putStrLn ("Volume: " ++ show (coneVolume cone))
putStrLn ("Surface Area: " ++ show (coneSurfaceArea cone))


### Output

Volume: 261.79938779914943
Surface Area: 254.160184615763


## Conclusion

There are different ways to calculate the volume, lateral area and surface area of a cone, but the mathematical formula to compute the volume and area of the cone will always remains the same. In Haskell, the volume and the area of a cone can be calculated by using user-defined function or by defining Cone Data Type.