- 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 of Cube

This tutorial will help us in calculating the volume of a cube. The volume of a cube is a measure of the amount of space inside the cube. It is calculated by multiplying the length of one side of the cube (s) by itself three times. The formula to calculate the volume of a cube is −

V = s^3

Where V is the volume and s is the length of one side of the cube.

In other words, it's the cube of the side length.

## Method 1: Using volumeOfCube Function

In this method, the function volumeOfCube is defined that takes a single Float argument, side, and returns the cube of that side.

The main function takes length of one side of the cube as a Float and uses the volumeOfCube function to calculate the volume of cube and prints it.

### Algorithm

**Step 1**− The function volumeOfCube is being defined on the basis of simple mathematical formula i.e., side^3 as, volumeOfCube side = side * side * side.**Step 2**− Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.**Step 3**− A variable named, “side” is being initialized. It will contain the length of the side of the cube.**Step 4**− The final resultant volume value is displayed by using ‘putStrLn’ statement once the volumeOfCube function is called.

### Example

In this example, we are going to calculate the volume of Cube by using volumeOfCube function.

volumeOfCube :: Float -> Float volumeOfCube side = side * side * side main = do let side = 4.5 putStrLn ("The volume of the cube is: " ++ show (volumeOfCube side))

### Output

The volume of the cube is: 91.125

## Method 2: Using Let Keyword

In this method, the let keyword is used to bind the result of the calculation side * side * side to a variable volume. And then it uses the putStrLn function to print the string "The volume of the cube is: " concatenated with the result of the show function applied to volume. The show function is used to convert the value of volume to a string, so that it can be concatenated and printed to the console.

### Algorithm

**Step 1**− Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.**Step 2**− A variable named, “side” is being initialized. It will contain the length of the side of the cube.**Step 3**− A variable named, “volume” is being initialized using let keyword. It will hold the formula to compute the volume of the cube and could be defined as, let volume = side * side * side. The let keyword is used to bind the result of the calculation side * side * side to a variable volume.**Step 4**− The final resultant volume value is displayed by using ‘putStrLn’ statement and show function.

### Example

In this example, we are going to calculate the volume of Cube by using let keyword.

main = do let side = 4.5 let volume = side * side * side putStrLn ("The volume of the cube is: " ++ show volume)

### Output

The volume of the cube is: 91.125

## Method 3: Using Where Keyword

This method uses the where keyword to define a function volume that takes a single argument s and returns the cube of that s. And then it calls the function volume with the value of side to get the volume of cube. Finally, the show function is used to convert the value of volume side to a string, so that it can be concatenated and printed to the console.

### Algorithm

**Step 1**− Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.**Step 2**− A variable named, “side” is being initialized. It will contain the length of the side of the cube.**Step 3**− The putStrLn function is used to print the string "The volume of the cube is: " concatenated with the result of the show function applied to volume side. The volume is a function which takes single argument s and returns the cube of s by s*s*s.**Step 4**− The final resultant volume value is displayed to the console.

### Example

In this example, we are going to calculate the volume of Cube by using where keyword.

main = do let side = 4.5 putStrLn ("The volume of the cube is: " ++ show (volume side)) where volume s = s * s * s

### Output

The volume of the cube is: 91.125

## Conclusion

There are different ways to calculate the volume of a cube, but the mathematical formula to compute the volume of the cube will always remain the same i.e., (side)^3. In Haskell, the volume of a cube can be calculated by using volumeOfCube function or by using let and where keywords.