An Armstrong number is a number that is equal to the sum of its own digits raised to the power of the number of digits. Finding Armstrong numbers between two intervals is a common programming task. In this article, we’ll explore three Kotlin Program to Display Armstrong Number Between Two Intervals.
1. Using Iterative Approach
This example demonstrates how to find and display Armstrong numbers between two intervals using an iterative approach. We iterate through each number in the given range, calculate the sum of its digits raised to the power of the number of digits, and check if it’s equal to the original number.
Code
fun main() {
val start = 100
val end = 999
println("Armstrong numbers between $start and $end:")
for (num in start..end) {
var temp = num
var sum = 0
val digits = num.toString().length
while (temp != 0) {
val digit = temp % 10
sum += Math.pow(digit.toDouble(), digits.toDouble()).toInt()
temp /= 10
}
if (sum == num) {
println(num)
}
}
}
Output
Armstrong numbers between 100 and 999:
153
370
371
407
2. Using Functional Approach
In this example, we use a functional programming approach to find and display Armstrong numbers between two intervals. We generate a list of numbers in the given range and then filter out the Armstrong numbers using a custom function.
Code
fun main() {
val start = 100
val end = 999
println("Armstrong numbers between $start and $end:")
val armstrongNumbers = (start..end).filter { isArmstrongNumber(it) }
armstrongNumbers.forEach { println(it) }
}
fun isArmstrongNumber(num: Int): Boolean {
var temp = num
var sum = 0
val digits = num.toString().length
while (temp != 0) {
val digit = temp % 10
sum += Math.pow(digit.toDouble(), digits.toDouble()).toInt()
temp /= 10
}
return sum == num
}
Output
Armstrong numbers between 100 and 999:
153
370
371
407
3. Using Kotlin’s Extension Functions
3.1 Explanation
Here, we create an extension function isArmstrong
for Integers in Kotlin to check if a number is an Armstrong number. We then use this extension function to find Armstrong numbers between two intervals.
Code
fun Int.isArmstrong(): Boolean {
var temp = this
var sum = 0
val digits = this.toString().length
while (temp != 0) {
val digit = temp % 10
sum += Math.pow(digit.toDouble(), digits.toDouble()).toInt()
temp /= 10
}
return sum == this
}
fun main() {
val start = 100
val end = 999
println("Armstrong numbers between $start and $end:")
val armstrongNumbers = (start..end).filter { it.isArmstrong() }
armstrongNumbers.forEach { println(it) }
}
3.3 Output
Armstrong numbers between 100 and 999:
153
370
371
407
4. Conclusion
Finding Armstrong numbers between two intervals is a classic programming problem, and Kotlin offers various approaches to tackle it efficiently. Whether using an iterative approach, a functional programming approach, or Kotlin’s extension functions, understanding these solutions enhances your Kotlin programming skills and prepares you for handling mathematical computations effectively in your applications. Each example presented here provides a unique insight into Kotlin’s versatility in solving mathematical problems, giving you the flexibility to choose the most suitable approach based on your specific requirements.