Kotlin Program to Compute Quotient and Remainder

In this article, we will understand how to compute the quotient and reminder in Kotlin. Quotient and Reminder is calculated using two simple formulae ?

"Quotient = Dividend / Divisor"
"Remainder = Dividend % Divisor"

Below is a demonstration of the same

Suppose our input is ?

Dividend value: 50
Divisor: 3

The desired output would be ?

Quotient: 16
Remainder: 2

Algorithm

  • Step 1 ? Start

  • Step 2 ? Declare four integers as myDividend , myDivisor, resultQuotient, resultRemainder

  • Step 3 ? Define the integers

  • Step 4 ? Use the formula to find the quotient and the reminder "Quotient = Dividend / Divisor" and "Remainder = Dividend % Divisor"

  • Step 5 ? Display the result

  • Step 6 ? Stop

Example 1

In this example, we will calculate the Quotient and Remainder. First, set and initialize the values for Dividend and Divisor;

val myDividend = 50
val myDivisor = 3

Now, find the quotient and remainder using the above given formulae ?

val resultQuotient = myDividend / myDivisor
val resultRemainder = myDividend % myDivisor

Let us see the example to compute the quotient and reminder in Kotlin ?

<div class="execute"></div><div class="code-mirror  language-kotlin" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">val</span> myDividend <span class="token operator">=</span> <span class="token number">50</span>
   <span class="token keyword">val</span> myDivisor <span class="token operator">=</span> <span class="token number">3</span>
   
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The Dividend value is defined as <span class="token interpolation variable">$myDividend</span> and Divisor value is defined as <span class="token interpolation variable">$myDivisor</span> "</span><span class="token punctuation">)</span>
   
   <span class="token keyword">val</span> resultQuotient <span class="token operator">=</span> myDividend <span class="token operator">/</span> myDivisor
   <span class="token keyword">val</span> resultRemainder <span class="token operator">=</span> myDividend <span class="token operator">%</span> myDivisor
   
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The quotient is <span class="token interpolation variable">$resultQuotient</span>"</span><span class="token punctuation">)</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The remainder is <span class="token interpolation variable">$resultRemainder</span>"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

The Dividend value is defined as 50 and Divisor value is defined as 3
The quotient is 16
The remainder is 2

Example 2

In this example, we will compute Quotient and Remainder with a custom function ?

<div class="execute"></div><div class="code-mirror  language-kotlin" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">val</span> myDividend <span class="token operator">=</span> <span class="token number">50</span>
   <span class="token keyword">val</span> myDivisor <span class="token operator">=</span> <span class="token number">3</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The Dividend value is defined as <span class="token interpolation variable">$myDividend</span> and Divisor value is defined as <span class="token interpolation variable">$myDivisor</span> "</span><span class="token punctuation">)</span>
   <span class="token function">getValues</span><span class="token punctuation">(</span>myDividend<span class="token punctuation">,</span> myDivisor<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">fun</span> <span class="token function">getValues</span><span class="token punctuation">(</span>myDividend<span class="token operator">:</span> Int<span class="token punctuation">,</span> myDivisor<span class="token operator">:</span> Int<span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">val</span> resultQuotient <span class="token operator">=</span> myDividend <span class="token operator">/</span> myDivisor
   <span class="token keyword">val</span> resultRemainder <span class="token operator">=</span> myDividend <span class="token operator">%</span> myDivisor
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The quotient is <span class="token interpolation variable">$resultQuotient</span>"</span><span class="token punctuation">)</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The remainder is <span class="token interpolation variable">$resultRemainder</span>"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

The Dividend value is defined as 50 and Divisor value is defined as 3
The quotient is 16
The remainder is 2

Example 3

In this example, we will calculate the Quotient and Remainder of a negative number

<div class="execute"></div><div class="code-mirror  language-kotlin" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">fun</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   <span class="token keyword">val</span> myDividend <span class="token operator">=</span> <span class="token operator">-</span><span class="token number">40</span>
   <span class="token keyword">val</span> myDivisor <span class="token operator">=</span> <span class="token number">7</span>
   
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The Dividend value is defined as <span class="token interpolation variable">$myDividend</span> and Divisor value is defined as <span class="token interpolation variable">$myDivisor</span> "</span><span class="token punctuation">)</span>
   
   <span class="token keyword">val</span> resultQuotient <span class="token operator">=</span> myDividend <span class="token operator">/</span> myDivisor
   <span class="token keyword">val</span> resultRemainder <span class="token operator">=</span> myDividend <span class="token operator">%</span> myDivisor
   
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The quotient is <span class="token interpolation variable">$resultQuotient</span>"</span><span class="token punctuation">)</span>
   <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"The remainder is <span class="token interpolation variable">$resultRemainder</span>"</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

The Dividend value is defined as -40 and Divisor value is defined as 7
The quotient is -5
The remainder is -5
Updated on: 2022-10-13T13:09:04+05:30

748 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements