Haskell Program to calculate the volume and area of the Cylinder


This tutorial will help us in calculating the volume and area of the Cylinder. The volume of a cylinder is a measure of the amount of space inside the cylinder. The area involves the surface area of the cylinder. The formula for the volume of a cylinder is the product of the base area of the cylinder, which is given by πr^2 and the height h. The formula for the surface area of a cylinder is the sum of the areas of the two circular faces and the area of the rectangular lateral surface.

Algorithm

  • Step 1 − The Text.Printf module is imported.

  • Step 2 − The volume and area functions are defined on the basis of simple mathematical formula as volume r h = pi * r * r * h and area r h = (2 * pi * r * h) + (2 * pi * r * r) 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 the radius and height of the cylinder and then calls the volume and area functions to calculate the results.

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

  • Step 5 − The final resultant volume and area value is displayed by using ‘printf’ statement once the volume and area functions are called.

 Using User-defined Area and Volume Function

In this example, the two functions are defined as, volume and area, which take the radius and height of a cylinder as arguments and return its volume and surface area, respectively.

Example 1

import Text.Printf

volume :: Double -> Double -> Double
volume r h = pi * r * r * h

area :: Double -> Double -> Double
area r h = (2 * pi * r * h) + (2 * pi * r * r)

main :: IO ()
main = do
   let radius = 5
   let height = 10
   let vol = volume radius height
   let ar = area radius height
   printf "Volume of the cylinder is: %.2f
" vol printf "Surface area of the cylinder is: %.2f
" ar

Output

Volume of the cylinder is: 785.40
Surface area of the cylinder is: 471.24

Using $ Operator and Print Function

This approach uses the $ operator to apply the function on the right to the argument on the left, effectively reducing the need for parentheses around the function call. Finally, it uses the print function to print the result to the console.

Example 2

import Text.Printf

volume :: Double -> Double -> Double
volume r h = pi * r * r * h

area :: Double -> Double -> Double
area r h = (2 * pi * r * h) + (2 * pi * r * r)

main :: IO ()
main = do
   let radius = 5
   let height = 10
   print $ "Volume of the cylinder is: " ++ show (volume radius height)
   print $ "Surface area of the cylinder is: " ++ show (area radius height)

Output

"Volume of the cylinder is: 785.3981633974483"
"Surface area of the cylinder is: 471.23889803846896"

Conclusion

There are different examples to calculate the volume and area of a cylinder, but the mathematical formula to compute the volume and area of the cylinder will always remains the same. In Haskell, the volume and the area of a cylinder can be calculated by using user-defined function or by using $ operator and print function.

Updated on: 23-Jan-2023

171 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements