Kotlin Program to find remainder without using the modulus operator


In this program, You will learn how to find the remainder without using the modulus operator in Kotlin.


1 = 5 % 2

2 = 2 % 5

Example: How to find the remainder without using the modulus operator in Kotlin.

import java.util.Scanner

fun main(args: Array<String>) {

    var x: Int
    var y: Int

    var sc = Scanner(System.`in`)

    print("Enter two numbers:")
    x = sc.nextInt()
    y = sc.nextInt()

    while (x >= y) {
        x -= y
    }

    println("Remainder is:$x")
}

Output:

Enter two numbers:10 3
Remainder is:1