# Haskell Program to get the Division and Remainder using library function

In Haskell, we can use divMod and quotRem functions to get the division and remainder of a given number. In the first example, we are going to use (divMod x y) function and in the second example, we are going to use (quotRem 20 7) function.

### 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 − The variables named, “x” and “y” are being initialized. The interal function will divide them by taking them as argument.

• Step 3 − The resultant quotient and remainder value is printed to the console using ‘putStrLn’ statement after the function is called.

### Example 1

In this example, we are going to see that how we can get the quotient and remainder after the division of two numbers. This can be done by using divMod function.

main :: IO ()
main = do
let x = 10
let y = 3
let (quotient, remainder) = divMod x y
putStrLn $"Quotient: " ++ show quotient putStrLn$ "Remainder: " ++ show remainder


### Output

Quotient: 3
Remainder: 1


### Example 2

In this example, we are going to see that how we can get the quotient and remainder after the division of two numbers. This can be done by using div and Mod function separately.

main :: IO ()
main = do
let x = 10
let y = 3
let quotient = x div y
let remainder = x mod y
putStrLn $"Quotient: " ++ show quotient putStrLn$ "Remainder: " ++ show remainder


### Output

Quotient: 3
Remainder: 1


### Example 3

In this example, we are going to see that how we can get the quotient and remainder after the division of two numbers. This can be done by using divMod function on tuple.

import Data.Tuple

main = do
let (q, r) = divMod 20 7
print (q, r)


### Output

(2, 6)


### Example 4

In this example, we are going to see that how we can get the quotient and remainder after the division of two numbers. This can be done by using quotRem function.

main = do
let (q, r) = quotRem 20 7
print (q, r)


### Output

(2,6)


### Example 5

In this example, we are going to see that how we can get the quotient and remainder after the division of two numbers. This can be done by using quot and Rem functions separately.

main = do
let q = 20 quot 7
let r = 20 rem 7
print (q, r)


### Output

(2,6)


## Conclusion

The quotient is the result of dividing one number by another and it is the integer part of the division. And remainder is the amount left over after dividing one number by another and it is the remainder part of the division. In Haskell, to get the quotient and remainder on dividing two numbers, we can use the divMod or quotRem functions together or separately.

Updated on: 13-Mar-2023

506 Views 