Kotlin Program to Check if a Number is Armstrong

In this blog post, we will learn how to write a Kotlin program to check if a number is Armstrong.

In programming, checking if a number is an Armstrong number is a common task. An Armstrong number is a number that is equal to the sum of its digits raised to the power of the number of digits. In this blog post, we will explore a Kotlin program that efficiently determines whether a given number is an Armstrong number. We will walk through the program step by step, explaining the logic behind it.

Kotlin Program: Checking if a Number is Armstrong

To check if a number is an Armstrong number in Kotlin, we can follow a straightforward approach. Let's dive into the code:

fun isArmstrongNumber(number: Int): Boolean {
    var originalNumber = number
    var result = 0
    val power = number.toString().length

    while (originalNumber != 0) {
        val remainder = originalNumber % 10
        result += Math.pow(remainder.toDouble(), power.toDouble()).toInt()
        originalNumber /= 10
    }

    return result == number
}

fun main() {
    val number = 153

    val isArmstrong = isArmstrongNumber(number)

    if (isArmstrong) {
        println("$number is an Armstrong number.")
    } else {
        println("$number is not an Armstrong number.")
    }
}

Output:

153 is an Armstrong number.
Explanation: 
The isArmstrongNumber() function takes an integer number as input and returns a Boolean value indicating whether the number is an Armstrong number. 

We initialize a variable originalNumber with the input number to preserve the original value. 

We initialize a variable result to store the sum of the digits raised to the power of the number of digits.

Using the toString().length property, we determine the number of digits in the input number and store it in the power variable. 

We enter a while loop that continues until originalNumber becomes 0. Inside the loop, we calculate the remainder of originalNumber divided by 10 using the modulo operator. 

We add the result of raising the remainder to the power of power using Math.pow() and convert it to an integer using toInt()

We add this value to the result variable. We update originalNumber by dividing it by 10 to remove the last digit. 

After the loop, we check if the result is equal to the original number. If they are equal, the number is an Armstrong number, and we return true. Otherwise, we return false. 

In the main() function, we create a sample input number (val number = 153) and call the isArmstrongNumber() function with this number. The function's result is stored in the isArmstrong variable, and we use an if-else statement to print whether the number is an Armstrong number or not.

Conclusion

In this blog post, we have discussed a Kotlin program that efficiently checks if a given number is an Armstrong number. By manipulating the digits of the number and comparing the sum of the powered digits, we can determine if the number meets the Armstrong criteria. Understanding this program equips you with the necessary skills to solve similar number-related problems in Kotlin. 

Feel free to explore and modify the code to suit your specific needs. The concept of Armstrong numbers is valuable in various programming scenarios, including number theory, mathematical computations, and algorithmic challenges. Happy coding!

Comments