Write a kotlin program to find perfect number or not.

A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself. Equivalently, a perfect number is a number that is half the sum of all of its positive divisors.

The first perfect number is 6, because 1, 2 and 3 are its proper positive divisors, and 1 + 2 + 3 = 6. Equivalently, the number 6 is equal to half the sum of all its positive divisors: ( 1 + 2 + 3 + 6 ) / 2 = 6

Kotlin program to find perfect number or not


fun isPerfectNumber(number: Int): Boolean {

    var temp = 0
    for (i in 1..number / 2) {
        if (number % i == 0) {
            temp += i
        }
    }
    if (temp == number) {
        println("It is a perfect number")
        return true
    } else {
        println("It is not a perfect number")
        return false
    }
}

fun main(a: Array<String>) {
    var num = 28
    println("Given number is : " +num)
    println("Is perfect number : " + isPerfectNumber(num))
}


Output


It is a perfect number
Is perfect number : true

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions