Write a program to find the given number is Armstrong number or not?

Armstrong numbers are the sum of their own digits to the power of the number of digits. It is also known as narcissistic numbers.

Kotlin program to find the given number is Armstrong number or not


fun isArmstrongNumber(number: Int): Boolean {

    var tmp = number
    val noOfDigits = number.toString().length
    var sum = 0
    var div = 0
    while (tmp > 0) {
        div = tmp % 10
        var temp = 1
        for (i in 0 until noOfDigits) {
            temp *= div
        }
        sum += temp
        tmp = tmp / 10
    }
    return if (number == sum) {
        true
    } else {
        false
    }
}

fun main(a: Array<String>) {
    println("Is 371 Armstrong number? " + isArmstrongNumber(371))
    println("Is 523 Armstrong number? " + isArmstrongNumber(523))
    println("Is 153 Armstrong number? " + isArmstrongNumber(153))
}


Output


Is 371 Armstrong number? true
Is 523 Armstrong number? false
Is 153 Armstrong 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