- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Haskell Program to calculate the volume and area of Sphere

This tutorial will help us in calculating the volume and area of Sphere. The volume of a sphere is a measure of the amount of space inside the sphere. And area involves the surface area of the sphere.

## Method 1: Using the User-defined Function

In this method, we will see two examples where we have used user-defined function with different techniques.

### 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 = (4.0 / 3.0) * pi * (r ^ 3) and area r = 4 * pi * (r ^ 2) respectively.**Step 3**− Program execution will be started from main function. The main() function has whole control of the program. The main function takes the radius of the sphere, and then calls the volume and area functions to calculate the volume and area of the sphere.**Step 4**− The variable named, “r” is being initialized. It will contain the radius of the sphere whose surface area and volume is to be computed.**Step 5**− The final resultant volume and area value is displayed by using ‘printf’ statement.

### Example 1

This example defines two functions volume and area, which take a single parameter, the radius of the sphere, and return the volume and surface area of the sphere, respectively.

import Text.Printf volume :: Double -> Double volume r = (4.0 / 3.0) * pi * (r ^ 3) area :: Double -> Double area r = 4 * pi * (r ^ 2) main :: IO () main = do let r = 5.0 let v = volume r let a = area r printf "Volume of sphere: %.2f

" v printf "Surface area of sphere: %.2f

" a

### Output

Volume of sphere: 523.60 Surface area of sphere: 314.16

### Example 2

In this example, the sphere function takes a single parameter, the radius of the sphere, and returns a tuple of the volume and surface area of the sphere. The main function calls the sphere function directly and uses pattern matching to assign the results to v and a.

import Text.Printf sphere :: Double -> (Double, Double) sphere r = ((4.0 / 3.0) * pi * (r ^ 3), 4 * pi * (r ^ 2)) main :: IO () main = do let r = 5.5 let (v, a) = sphere r printf "Volume of sphere: %.2f

" v printf "Surface area of sphere: %.2f

" a

### Output

Volume of sphere: 696.91 Surface area of sphere: 380.13

## Method 2: Using Let Keyword

This approach eliminates the need for separate functions to calculate the volume and area. The main function takes the radius of the sphere, reads the input and then uses the let keyword to bind the results of the calculations to the variables v and a. The results are then printed to the screen with two decimal places of precision.

### Algorithm

**Step 1**− The Text.Printf module is imported.**Step 2**− Program execution will be started from main function. The main() function has whole control of the program. The main function takes the radius of the sphere, and then calls the volume and area functions to calculate the volume and area of the sphere.**Step 3**− The single sphere function is defined as, sphere r = ((4.0 / 3.0) * pi * (r ^ 3), 4 * pi * (r ^ 2)).**Step 4**− The variable named, “r” is being initialized. It will contain the radius of the sphere. And variable “v” & “a” are initialized to contain the formula for calculating volume and area of the sphere respectively.**Step 5**− The final resultant volume and area value is displayed by using ‘printf’ statement.

### Example 3

import Text.Printf main :: IO () main = do let r = 5.0 :: Double let v = (4.0 / 3.0) * pi * (r ^ 3) :: Double let a = 4 * pi * (r ^ 2) :: Double printf "Volume of sphere with radius %.2f is: %.2f

" r v printf "Surface area of sphere with radius %.2f is: %.2f

" r a

### Output

Volume of sphere with radius 5.00 is: 523.60 Surface area of sphere with radius 5.00 is: 314.16

## Conclusion

There are different ways to calculate the volume and area of a sphere, but the mathematical formula to compute the volume and area of the sphere will always remain the same. In Haskell, the volume and the area of a sphere can be calculated by using user-defined multiple functions or single function, or by using the let keyword.

- Related Articles
- Swift Program to calculate the volume and area of Sphere
- C++ Program to calculate the volume and area of Sphere
- Python Program to calculate the volume and area of Sphere
- Golang program to calculate the volume and area of a Sphere
- Haskell Program to calculate the volume and area of Cone
- Haskell Program to calculate the volume and area of the Cylinder
- Haskell Program to calculate the volume, diagonal, and area of Cuboids
- Haskell Program to calculate the volume of Cube
- Haskell Program to Find the Surface area and Volume of Cuboid
- Haskell Program to calculate the area of Cube
- Python Program to calculate the volume and area of Cone
- Haskell Program to calculate the area of the rhombus
- Program to calculate area and volume of a Tetrahedron
- Swift Program to calculate the volume and area of the Cylinder
- Python Program to calculate the volume and area of the Cylinder