# Haskell Program to calculate the area of the rhombus

In Haskell there are different methods to calculating the area of the rhombus. We can use sides, diagonals and height on the basis of which, its area can be computed by various methods.

### Algorithm

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

• Step 2 − Defined the Rhombus fuction

• 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. It takes two integers as diagonals and prints the area using the rhombusArea function.

• Step 4 − The variables named, “d1” and “d2” are initialized. It will hold the two integers as diagonals of the rhombus.

• Step 5 − The resultant area of rhombus is printed to the console using ‘printf’ function. The printf function is used to format the output to two decimal places.

### Example 1

In this example, a function rhombusArea is defined that takes in the two diagonals of the rhombus as arguments and returns the area using the formula (d1 * d2) / 2.

import Text.Printf

rhombusArea :: Double -> Double -> Double
rhombusArea d1 d2 = (d1 * d2) / 2

main :: IO ()
main = do
let d1 = 4
let d2 = 5

let area = rhombusArea d1 d2
printf "Area of the rhombus: %.2f" area


### Output

Area of the rhombus: 10.00


### Example 2

In this example, the length of the sides of the rhombus are taken as input and the formula used to calculate the area is (s1*s2)/2.

import Text.Printf

rhombusArea :: Double -> Double -> Double
rhombusArea s1 s2 = (s1 * s2) / 2

main :: IO ()
main = do
let s1 = 4
let s2 = 5
let area = rhombusArea s1 s2
printf "Area of the rhombus: %.2f" area


### Output

Area of the rhombus: 10.00


### Example 3

In this example, the length of one side and the height of the rhombus are taken as input, then the formula s*h is used to calculate the area.

import Text.Printf

rhombusArea :: Double -> Double -> Double
rhombusArea s h = s * h

main :: IO ()
main = do
let s = 4
let h = 5

let area = rhombusArea s h
printf "Area of the rhombus: %.2f" area


### Output

Area of the rhombus: 20.00


## Conclusion

The area of the rhombus in Haskell can be calculated by using its diagonal; sides; or by using one side & height. The area is printed to the console using ‘printf’ function on calling the function defined.

Updated on: 01-Mar-2023

67 Views