Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
<div class="execute"></div><div class="code-mirror language-haskell" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token hvariable">main</span> <span class="token operator">::</span> <span class="token constant">IO</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token hvariable">main</span> <span class="token operator">=</span> <span class="token keyword">do</span> <span class="token comment">-- declaring and initializing operand variables</span> <span class="token keyword">let</span> <span class="token hvariable">dividend</span> <span class="token operator">=</span> <span class="token number">31</span> <span class="token keyword">let</span> <span class="token hvariable">divisor</span> <span class="token operator">=</span> <span class="token number">5</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"Entered Dividend and Divisor are:"</span><span class="token punctuation">)</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token builtin">show</span> <span class="token hvariable">dividend</span> <span class="token operator">++</span><span class="token string">"and"</span><span class="token operator">++</span> <span class="token builtin">show</span> <span class="token hvariable">divisor</span><span class="token punctuation">)</span> <span class="token comment">-- computing quotient using function div and loading into a variable</span> <span class="token keyword">let</span> <span class="token hvariable">quotient</span> <span class="token operator">=</span> <span class="token builtin">div</span> <span class="token hvariable">dividend</span> <span class="token hvariable">divisor</span> <span class="token comment">-- computing remainder using funcion mod and loading into a variable</span> <span class="token keyword">let</span> <span class="token hvariable">remainder</span> <span class="token operator">=</span> <span class="token builtin">mod</span> <span class="token hvariable">dividend</span> <span class="token hvariable">divisor</span> <span class="token comment">-- printing the quotient and remainder</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"So the Quotient and Remainder are:"</span><span class="token punctuation">)</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token builtin">show</span> <span class="token hvariable">quotient</span> <span class="token operator">++</span><span class="token string">"and"</span><span class="token operator">++</span> <span class="token builtin">show</span> <span class="token hvariable">remainder</span><span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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
<div class="execute"></div><div class="code-mirror language-haskell" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token hvariable">main</span> <span class="token operator">::</span> <span class="token constant">IO</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token hvariable">main</span> <span class="token operator">=</span> <span class="token keyword">do</span> <span class="token comment">-- declaring and initializing operand variables</span> <span class="token keyword">let</span> <span class="token hvariable">dividend</span> <span class="token operator">=</span> <span class="token number">31</span> <span class="token keyword">let</span> <span class="token hvariable">divisor</span> <span class="token operator">=</span> <span class="token number">5</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"Entered Dividend and Divisor are:"</span><span class="token punctuation">)</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token builtin">show</span> <span class="token hvariable">dividend</span> <span class="token operator">++</span><span class="token string">"and"</span><span class="token operator">++</span> <span class="token builtin">show</span> <span class="token hvariable">divisor</span><span class="token punctuation">)</span> <span class="token comment">-- computing quotient using division operator</span> <span class="token keyword">let</span> <span class="token hvariable">quotient</span> <span class="token operator">=</span> <span class="token builtin">floor</span> <span class="token punctuation">(</span><span class="token hvariable">dividend</span> <span class="token operator">/</span> <span class="token hvariable">divisor</span><span class="token punctuation">)</span> <span class="token comment">-- computing remainder</span> <span class="token keyword">let</span> <span class="token hvariable">remainder</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token builtin">round</span> <span class="token hvariable">dividend</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token punctuation">(</span><span class="token builtin">round</span> <span class="token hvariable">divisor</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token hvariable">quotient</span> <span class="token comment">-- printing the quotient and remainder</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"So the Quotient and Remainder are:"</span><span class="token punctuation">)</span> <span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token builtin">show</span> <span class="token hvariable">quotient</span> <span class="token operator">++</span><span class="token string">"and"</span><span class="token operator">++</span> <span class="token builtin">show</span> <span class="token hvariable">remainder</span><span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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
<div class="execute"></div><div class="code-mirror language-haskell" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">-- function declaration</span>
<span class="token hvariable">remainder</span> <span class="token operator">::</span> <span class="token constant">Int</span><span class="token operator">-></span><span class="token constant">Int</span><span class="token operator">-></span><span class="token constant">Int</span>
<span class="token comment">-- function definition</span>
<span class="token hvariable">remainder</span> <span class="token hvariable">dividend</span> <span class="token hvariable">divisor</span> <span class="token operator">=</span> <span class="token keyword">if</span> <span class="token hvariable">divisor</span> <span class="token operator">>=</span> <span class="token hvariable">dividend</span>
<span class="token keyword">then</span> <span class="token hvariable">dividend</span>
<span class="token keyword">else</span> <span class="token hvariable">remainder</span> <span class="token punctuation">(</span><span class="token hvariable">dividend</span><span class="token operator">-</span><span class="token hvariable">divisor</span><span class="token punctuation">)</span> <span class="token hvariable">divisor</span>
<span class="token hvariable">main</span> <span class="token operator">::</span> <span class="token constant">IO</span> <span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token hvariable">main</span> <span class="token operator">=</span> <span class="token keyword">do</span>
<span class="token comment">-- declaring and initializing operand variables</span>
<span class="token keyword">let</span> <span class="token hvariable">dividend</span> <span class="token operator">=</span> <span class="token number">31</span>
<span class="token keyword">let</span> <span class="token hvariable">divisor</span> <span class="token operator">=</span> <span class="token number">5</span>
<span class="token comment">-- invoking remainder function and printing result</span>
<span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"The remainder is:"</span><span class="token punctuation">)</span>
<span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token hvariable">remainder</span> <span class="token hvariable">dividend</span> <span class="token hvariable">divisor</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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
<div class="execute"></div><div class="code-mirror language-haskell" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">-- function declaration</span>
<span class="token hvariable">quotient</span> <span class="token operator">::</span> <span class="token constant">Int</span><span class="token operator">-></span><span class="token constant">Int</span><span class="token operator">-></span><span class="token constant">Int</span><span class="token operator">-></span><span class="token constant">Int</span>
<span class="token comment">-- function definition</span>
<span class="token hvariable">quotient</span> <span class="token hvariable">dividend</span> <span class="token hvariable">divisor</span> <span class="token hvariable">cnt</span> <span class="token operator">=</span> <span class="token keyword">if</span> <span class="token hvariable">divisor</span> <span class="token operator">>=</span> <span class="token hvariable">dividend</span>
<span class="token keyword">then</span> <span class="token hvariable">cnt</span>
<span class="token keyword">else</span> <span class="token hvariable">quotient</span> <span class="token punctuation">(</span><span class="token hvariable">dividend</span><span class="token operator">-</span><span class="token hvariable">divisor</span><span class="token punctuation">)</span> <span class="token hvariable">divisor</span> <span class="token hvariable">cnt</span><span class="token operator">+</span><span class="token number">1</span>
<span class="token hvariable">main</span> <span class="token operator">::</span> <span class="token constant">IO</span> <span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token hvariable">main</span> <span class="token operator">=</span> <span class="token keyword">do</span>
<span class="token comment">-- declaring and initializing operand variables</span>
<span class="token keyword">let</span> <span class="token hvariable">dividend</span> <span class="token operator">=</span> <span class="token number">31</span>
<span class="token keyword">let</span> <span class="token hvariable">divisor</span> <span class="token operator">=</span> <span class="token number">5</span>
<span class="token comment">-- invoking quotient function and printing result</span>
<span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token string">"The quotient is:"</span><span class="token punctuation">)</span>
<span class="token builtin">print</span> <span class="token punctuation">(</span><span class="token hvariable">quotient</span> <span class="token hvariable">dividend</span> <span class="token hvariable">divisor</span> <span class="token number">0</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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.
