Friday, November 8, 2019

Kotlin Program to Compute Quotient and Remainder

In the above program Kotlin Program to Compute Quotient and Remainder, two numbers 25 (dividend) and 4 (divisor) are stored in the dividend and division numbers respectively. Unlike Java, they are automatically assigned type Int in Kotlin.
Kotlin Program to Compute Quotient and Remainder

Now, to find the quotient, we divide the dividend by the divisor using / operator. Because, both dividends and divisors are Int, the result will also be calculated as Int.

So, mathematically even if April 25 has a result of 6.25, because both operands are Int, the quotient stores only 6 (whole numbers).


Similarly, to find the rest, we use the% operator. So the rest of the day 25/4, ie 1 is stored in the rest of the Int variable.

Finally, the target and the rest are printed on the screen using the println () function.
fun main(args: Array<String>) {
    val dividend = 25
    val divisor = 4
    val quotient = dividend / divisor
    val remainder = dividend % divisor
    println("Quotient = $quotient")
    println("Remainder = $remainder")
}
When you run the program, the output will be:
Quotient = 6
Remainder = 1

No comments:

Post a Comment