Haskell program to compute quotient and remainder


In this tutorial, we discuss writing a program to compute the quotient and remainder in Haskell programming language.

Quotient and remainder are the quantities obtained as a result of dividing two quantities.

Example − quotient and remainder for the division of 31/5 are 6,1 respectively. The number can be represented as 31 (dividend) = 5(divisor) * 6(quotient) + 1(remainder).

In this tutorial, we see four different ways to write a program to compute quotient and remainder.

  • Haskell program to compute the quotient and the remainder using built-in functions div and mod

  • Haskell program to compute the quotient and the remainder using built-in operators.

  • Haskell program to compute the remainder using a recursive function.

  • Haskell program to compute the quotient using recursive function.

Algorithm steps

  • Initialize or take dividend and divisor as input.

  • Implement the logic to compute the quotient and remainder.

  • Print or Display the computed result.

Example 1

Haskell program to compute quotient and remainder using built-in functions.

main :: IO() main = do -- declaring and initializing operand variables let dividend = 31 let divisor = 5 print ("Entered Dividend and Divisor are:") print (show dividend ++"and"++ show divisor) -- computing quotient using function div and loading into a variable let quotient = div dividend divisor -- computing remainder using funcion mod and loading into a variable let remainder = mod dividend divisor -- printing the quotient and remainder print ("So the Quotient and Remainder are:") print (show quotient ++"and"++ show remainder)

Output

"Entered Dividend and Divisor are:"
"31and5"
"So the Quotient and Remainder are:"
"6and1"

In the above program, we declared and initialized two variables dividend and divisor. We computed the quotient using the function div, which takes two arguments and returns the quotient for the computed division. We computed the remainder using the function mod, which takes two arguments and returns the remainder for the computed division. Finally, we printed the quotient and remainder using the print function. Note: The show is a function that takes a number as an argument, parses it into a string, and returns the string. “++” is an operator to concatenate two strings.

Example 2

Haskell program to compute quotient and remainder using operators

main :: IO() main = do -- declaring and initializing operand variables let dividend = 31 let divisor = 5 print ("Entered Dividend and Divisor are:") print (show dividend ++"and"++ show divisor) -- computing quotient using division operator let quotient = floor (dividend / divisor) -- computing remainder let remainder = (round dividend) - (round divisor)*quotient -- printing the quotient and remainder print ("So the Quotient and Remainder are:") print (show quotient ++"and"++ show remainder)

Output

"Entered Dividend and Divisor are:"
"31.0 and 5.0"
"So the Quotient and Remainder are:"
"6 and 1"

In the above program, we declared and initialized two variables dividend and divisor. We computed the quotient using the division operator(‘/’). As the returned number can be a decimal we used the function floor to extract the greatest integer less than that number which is a quotient for the division. The floor is a function that takes a number and returns the greatest integer less than or equal to that number. When we initialized numbers using the let keyword numbers are stored as float. So function round is used to typecast the float numbers to int. We computed the remainder by subtracting the divisor*quotient from the dividend. Finally, we printed the quotient and remainder.

Example 3

Haskell program to compute the remainder using a recursive function

-- function declaration remainder :: Int->Int->Int -- function definition remainder dividend divisor = if divisor >= dividend then dividend else remainder (dividend-divisor) divisor main :: IO () main = do -- declaring and initializing operand variables let dividend = 31 let divisor = 5 -- invoking remainder function and printing result print ("The remainder is:") print (remainder dividend divisor)

Output

"The remainder is:"
1

In the above program, we declared a function remainder as such it takes two integer arguments and returns an integer. In its function definition, we are accepting two arguments dividend and divisor. If the dividend is greater than the devisor, a recursive call to itself is made with arguments (dividend-divisor) and divisor. Else dividend is returned. The dividend returned is the remainder. In the main function, we are invoking this function and the returned remainder is printed.

Example 4

Haskell program to compute the quotient using a recursive function

-- function declaration quotient :: Int->Int->Int->Int -- function definition quotient dividend divisor cnt = if divisor >= dividend then cnt else quotient (dividend-divisor) divisor cnt+1 main :: IO () main = do -- declaring and initializing operand variables let dividend = 31 let divisor = 5 -- invoking quotient function and printing result print ("The quotient is:") print (quotient dividend divisor 0)

Output

"The quotient is:"
6

In the above program, function quotient is declared as such it takes three integer arguments and returns an integer. In its function definition, three arguments are accepted dividend, divisor, and cnt. If the dividend is greater than the divisor a recursive call to itself is invoked with arguments (dividend-divisor), divisor, and (cnt+1). Else the cnt value is returned. The function returns how many times the divisor can be subtracted from the dividend i.e quotient. In the main function, we invoked the quotient function with a dividend, divisor, and an initial count of 0. Finally, we printed the returned result.

Conclusion

In this tutorial, we discussed implementing a program to compute quotient and remainder in the Haskell programming Language.

Updated on: 24-Nov-2022

484 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements