An Armstrong number, also known as a narcissistic number, is a number that is equal to the sum of its own digits each raised to the power of the number of digits. For example, 153 is an Armstrong number because 1^3 + 5^3 + 3^3 = 153. In this article, we will explore different Kotlin Program to Check Armstrong Number. We will cover three different solutions, each with an example, and explain the output for each.
Prerequisites
- Kotlin Installed: Ensure Kotlin is installed on your system. Download from Kotlin’s official website.
- IDE: Use an Integrated Development Environment (IDE) like IntelliJ IDEA or Android Studio for running Kotlin programs.
- Basic Understanding of Kotlin: Familiarity with basic Kotlin syntax and concepts.
1. Simple Method to Check Armstrong Number
Program
fun isArmstrong(number: Int): Boolean {
var temp = number
val numberOfDigits = number.toString().length
var sum = 0
while (temp > 0) {
val digit = temp % 10
sum += Math.pow(digit.toDouble(), numberOfDigits.toDouble()).toInt()
temp /= 10
}
return sum == number
}
fun main() {
val number = 153
if (isArmstrong(number)) {
println("$number is an Armstrong number.")
} else {
println("$number is not an Armstrong number.")
}
}
1.2 Output
153 is an Armstrong number.
The isArmstrong
function checks if 153 is an Armstrong number by calculating the sum of its digits raised to the power of the number of digits. The sum matches the original number.
2. Using String Conversion to Check Armstrong Number
Program
fun isArmstrongString(number: Int): Boolean {
val digits = number.toString().map { it.toString().toInt() }
val numberOfDigits = digits.size
val sum = digits.map { Math.pow(it.toDouble(), numberOfDigits.toDouble()).toInt() }.sum()
return sum == number
}
fun main() {
val number = 370
if (isArmstrongString(number)) {
println("$number is an Armstrong number.")
} else {
println("$number is not an Armstrong number.")
}
}
Output
370 is an Armstrong number.
The isArmstrongString
function converts the number 370 to a string, processes each digit, and checks if the sum of the digits raised to the power of the number of digits equals the original number.
3. Using Recursion to Check Armstrong Number
Program
fun power(base: Int, exp: Int): Int {
return if (exp == 0) 1 else base * power(base, exp - 1)
}
fun isArmstrongRecursive(number: Int, numberOfDigits: Int): Boolean {
if (number == 0) return false
var temp = number
var sum = 0
fun calculateSum(temp: Int): Int {
return if (temp == 0) sum
else {
sum += power(temp % 10, numberOfDigits)
calculateSum(temp / 10)
}
}
val result = calculateSum(temp)
return result == number
}
fun main() {
val number = 9474
val numberOfDigits = number.toString().length
if (isArmstrongRecursive(number, numberOfDigits)) {
println("$number is an Armstrong number.")
} else {
println("$number is not an Armstrong number.")
}
}
3.2 Output
9474 is an Armstrong number.
The isArmstrongRecursive
function uses recursion to check if 9474 is an Armstrong number by calculating the sum of its digits raised to the power of the number of digits and comparing it with the original number.
Conclusion
Checking if a number is an Armstrong number can be done using different methods. The simple method involves basic iteration and modulus operations. The string conversion method leverages Kotlin’s string manipulation capabilities for simplicity. The recursive method offers a more functional approach. Each method has its own advantages and can be chosen based on the specific requirements and preferences.