Kotlin Program to Compute Quotient and Remainder

Kotlin Server Side ProgrammingProgramming

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 −

fun main() { val myDividend = 50 val myDivisor = 3 println("The Dividend value is defined as $myDividend and Divisor value is defined as $myDivisor ") val resultQuotient = myDividend / myDivisor val resultRemainder = myDividend % myDivisor println("The quotient is $resultQuotient") println("The remainder is $resultRemainder") }

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 −

fun main() { val myDividend = 50 val myDivisor = 3 println("The Dividend value is defined as $myDividend and Divisor value is defined as $myDivisor ") getValues(myDividend, myDivisor) } fun getValues(myDividend: Int, myDivisor: Int) { val resultQuotient = myDividend / myDivisor val resultRemainder = myDividend % myDivisor println("The quotient is $resultQuotient") println("The remainder is $resultRemainder") }

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

fun main() { val myDividend = -40 val myDivisor = 7 println("The Dividend value is defined as $myDividend and Divisor value is defined as $myDivisor ") val resultQuotient = myDividend / myDivisor val resultRemainder = myDividend % myDivisor println("The quotient is $resultQuotient") println("The remainder is $resultRemainder") }

Output

The Dividend value is defined as -40 and Divisor value is defined as 7
The quotient is -5
The remainder is -5
raja
Updated on 13-Oct-2022 13:09:04

Advertisements