# Haskell program to find the area of a circle

#### Complete Python Prime Pack

9 Courses     2 eBooks

#### Artificial Intelligence & Machine Learning Prime Pack

6 Courses     1 eBooks

#### Java Prime Pack

9 Courses     2 eBooks

This tutorial will discuss how to write a program to find the Area of the Circle in Haskell Programming Language. Haskell is a declarative, functional, and strongly typed Language. The computations in Haskell are done through Mathematical functions. Haskell programming language is inspired by lambda calculus.

In this tutorial we will discuss three different ways to calculate the area of a circle −

• Implementing the area computation in the main function

• Implementing the area computation in a separate function

• Implementing the area computation taking user input.

## Prerequisite

We know the formula for the area of a Circle is pi*r^2 where pi is constant 3.14 and r is the radius of the circle.

## Syntax

Following is the syntax structure to compute the area of the circle.

We start with a constant value pi and multiply it with a square of the radius.

pi * radius^2


Where pi is an inbuilt constant in Haskell representing the value ‘π’, the radius is a variable containing the radius of the circle.

Note − ‘*’ denotes multiplication computation and ‘^’ denotes power computation.

## Algorithm steps

• Define two variables for radius and result area

• Define a function to calculate the area of the Circle or write the logic in the main function.

• Load the result into the area variable

• Print the resultant area of the circle.

## Method 1- implementing the area logic in the main function

In the example program below, we will implement the area logic in the main function itself. Here we will declare a variable with the name radius and initialized it with the value 10. Then declare a variable result and initialized it with the area which pi multiplied by the radius square, and then printed the result.

### Example

main :: IO()
main = do
-- declaring variable 'radius' and initializing with 10

-- Haskell has an inbuilt constant for π denoted by pi
-- calculating area and loading the result in variable 'area'

-- printing the result
print "area of the circle is:"
print area


### Output

area of the circle is:
314.169265


Note − Single lines comments in Haskell are denoted by “--”

## Method 2- Implementing the area logic in a separate function

In this example, we will define a function named “areaOfCircle” which takes radius as an argument and returns the area of the circle as a result. In the main function, we will declare a variable named radius, and we initialized it with 10. Then we will declare a variable result and initialized it with the value returned by the function areaOfCircle which takes radius as the parameter. And finally, printing the area in the output.

### Example

-- function to return area of circle taking input radius as the first argument and returning area

main :: IO ()
main = do
-- declaring variable 'radius' and initializing with 10

--declaring variable area and populating result with the result of function areaOfcircle

-- printing area
print "area of the Circle is:"
print area


### Output

area of the circle is:
314.169265


## Method 3 - Calculating the area of the Circle, using diameter.

### Example

Example:
main :: IO ()
main = do
-- declaring variable for diameter
let diameter=2

-- computing area of the circle
let area= pi/4*diameter^2;

-- printing area
print "area of the Circle is:"
print area


### Output

"area of the Circle is:"
3.141592653589793


## Conclusion

So in this article, we have understood how to find area of a circle in Haskell Program using 3 different methods.

Updated on 11-Oct-2022 11:16:16