How to sum the proper divisors of a number in Kotlin

1 Answer

0 votes
fun sumNumberProperDivisors(num: Int): Int {
    var sum = 0

    for (i in 1..(num / 2)) {
        if (num % i == 0) {
            print("$i, ")
            sum += i
        }
    }

    return sum
}

fun main() {
    val sum = sumNumberProperDivisors(220)
    
    println("\n$sum")
}


 
  
/*
run:

1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110, 
284

*/

 



answered Aug 9, 2025 by avibootz

Related questions

1 answer 100 views
1 answer 131 views
1 answer 128 views
...