Kotlin Program to check a number is a perfect number or not


In this program, You will learn how to check a number is a perfect number or not in Kotlin.


Some list of perfect numbers is : 6, 28, 496, 8128

Example: How to check a number is a perfect number or not in Kotlin.

import java.util.Scanner

fun main(args: Array<String>) {

    var num: Int
    var i = 1
    var s = 0

    var sc = Scanner(System.`in`)

    print("Enter a number:")
    num = sc.nextInt()

    while (i < num) {
        if (num % i == 0) {
            s += i
        }
        i++
    }

    if (num == s) {
        println("It is a perfect number:$num")
    } else {
        println("It is not a perfect number:$num")
    }
}

Output:

Enter a number:28
It is a perfect number:28