- 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 find the largest among three numbers

In this tutorial, we discuss writing a program to find the largest among the three numbers Haskell programming language

We are going to learn three different ways to implement programs to find the largest among three numbers in Haskell.

Program to find the largest among three numbers using if-else.

Program to find the largest among three numbers using function max.

Program to find the largest among three numbers using function maximum.

### Syntax

variables in functions returning IO actions can be declared and initialized instantaneously using the “let” keyword.

let num=100

The above line loads an Integer value into num. We can load other data type values in the variable. Once initialized the data type strictly binds to the variable. The type of the variable is decided when it is initialized.

**The syntax for declaring and defining integers outside functions and functions not using IO operations**

num :: Int

The above line declares the variable num as Int.

**Note** − The above line is optional if data type declaration is not done data type binding occurs when it is initialized.

num = 10

The above line initializes the variable num with an integer value of 10. We cannot load the other data types as we declared the variable as an Integer. The compiler throws an error another data type is loaded. Here type of variable is decided at compile time.

### Algorithm steps

Initialize or take Three numbers as input.

Implement the logic to find the maximum among three numbers

Print or Display the maximum number among the three numbers.

### Example 1

Program to find the Largest among three numbers using if-else

-- function declaration findLargest :: Int->Int->Int->Int -- function definition findLargest a b c = if (a >= b) then if (a >= c) then a else c else if (b>=c) then b else c main :: IO() main = do -- declaring and initializing variables let a = 6 let b = 10 let c = 9 -- invoking the function findLargest and printing the result print ("largest among the three numbers:"++ show a ++" " ++ show b ++ " and " ++ show c ++ " is:") print (findLargest a b c)

### Output

"largest among the three numbers:6 10and 9 is:" 10

In the above program, we declared a function **findLargest** as such it takes three integers as arguments and returns an integer. In its function definition, we are taking three arguments a, b, and c. In the function, we implemented the logic to return the greatest among the three numbers using the if-else statements.

The logic is −

Initially we are comparing a and b. If a is greater than or equal to b.

We are comparing a and c. If a is greater than or equal to c.

we are returning the variable a

Else we are returning the variable c

If a is less than b,

we are comparing b and c, If b is greater than or equal to c

we are returning b

Else we are returning c.

In the main function, we are declaring and initializing three variables a,b, and c. We invoked the function **findLargest** with these three numbers as arguments. Finally, we printed the returned number using the **print** function.

**Note**− The **show** is a function used to parse numbers into strings.”++” is an operator to concatenate two strings.

### Example 2

Program to find the Largest among three numbers using the function max

main :: IO() main = do -- declaring and initializing variables let a = 6 let b = 10 let c = 9 -- computing the largest number using max function let largest = max (max a b) (max b c) -- printing the largest number print ("largest among the three numbers:"++ show a ++" " ++ show b ++ " and " ++ show c ++ " is:") print (largest)

### Output

"largest among the three numbers:6 10 and 9 is:" 10

In the above program, we declared and initialized three variables a, b, and c. We are computing the largest number using the function **max**, which takes two numbers as arguments and returns the largest number among them. We computed the largest among three numbers by computing the largest number among the largest numbers among a, b, and b, c using the function max. Finally, we printed the returned result.

### Example 3

Program to find the Largest among three numbers using the function maximum

main :: IO() main = do -- declaring and initializing variables let a = 6 let b = 10 let c = 9 -- computing the largest number using max function let largest = maximum [a,b,c] -- printing the largest number print ("largest among the three numbers:"++ show a ++" " ++ show b ++ " and " ++ show c ++ " is:") print (largest)

### Output

"largest among the three numbers:6 10 and 9 is:" 10

In the above program, we declared and initialized three variables a, b, and c. we used the function maximum which takes a list of numbers as argument and returns the maximum number in the list. We generated a list with the three variables and invoked the maximum function with this list as an argument and loaded the returned value into the variable **largest**. Finally, we printed the largest number.

## Conclusion

In this tutorial, we discussed implementing a program in three ways to find the largest number among three numbers in Haskell programming Language.

- Related Articles
- Java Program to Find the Largest Among Three Numbers
- Swift Program to Find the Largest Among Three Numbers
- Kotlin Program to Find the Largest Among Three Numbers
- C program to Find the Largest Number Among Three Numbers
- C++ Program to Find Largest Number Among Three Numbers
- How to Find the Largest Among Three Numbers in the Golang program?
- Java program to Largest number among three numbers
- How to Find the Largest Among Three Numbers using Python?
- Program to find largest of three numbers - JavaScript
- Java program to find largest of the three numbers using ternary operators
- C++ program to find the smallest element among three elements
- Haskell program to find the gcd of two numbers
- Haskell program to find lcm of two numbers
- Haskell Program to Swap Two Numbers
- Python program to find the maximum of three numbers