Kotlin Program to Check Armstrong Number
val sum = digits.map { it.toDouble().pow(n) }.sum().toInt() and then if (sum == number) println("Armstrong number") else println("Not Armstrong").Examples
How to Think About It
Algorithm
Code
import kotlin.math.pow fun main() { print("Enter a number: ") val number = readLine()!!.toInt() val digits = number.toString().map { it.toString().toInt() } val n = digits.size val sum = digits.map { it.toDouble().pow(n) }.sum().toInt() if (sum == number) { println("$number is an Armstrong number") } else { println("$number is not an Armstrong number") } }
Dry Run
Let's trace the number 153 through the code to see how it checks for Armstrong number.
Input number
number = 153
Extract digits
digits = [1, 5, 3]
Count digits
n = 3
Calculate sum of powers
sum = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
Compare sum with original
sum == number → 153 == 153 → true
Print result
"153 is an Armstrong number"
| Digit | Digit^3 |
|---|---|
| 1 | 1 |
| 5 | 125 |
| 3 | 27 |
Why This Works
Step 1: Extract digits
We convert the number to a string and then to a list of digits to handle each digit separately.
Step 2: Calculate power sum
Each digit is raised to the power of the total number of digits using pow and summed to check the Armstrong condition.
Step 3: Compare and decide
If the sum equals the original number, it confirms the number is Armstrong; otherwise, it is not.
Alternative Approaches
fun main() {
print("Enter a number: ")
val number = readLine()!!.toInt()
val n = number.toString().length
var sum = 0
var temp = number
while (temp > 0) {
val digit = temp % 10
sum += digit.toDouble().pow(n).toInt()
temp /= 10
}
if (sum == number) println("$number is an Armstrong number") else println("$number is not an Armstrong number")
}fun powerSum(num: Int, n: Int): Int {
if (num == 0) return 0
val digit = num % 10
return digit.toDouble().pow(n).toInt() + powerSum(num / 10, n)
}
fun main() {
print("Enter a number: ")
val number = readLine()!!.toInt()
val n = number.toString().length
val sum = powerSum(number, n)
if (sum == number) println("$number is an Armstrong number") else println("$number is not an Armstrong number")
}Complexity: O(d) time, O(d) space
Time Complexity
The program processes each digit once to calculate the power and sum, so time complexity is proportional to the number of digits, O(d).
Space Complexity
It uses extra space to store the digits list, which is O(d), where d is the number of digits.
Which Approach is Fastest?
The loop approach uses constant extra space and is slightly faster than map-based or recursive methods, but all are efficient for typical input sizes.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Map with pow | O(d) | O(d) | Readable and concise code |
| Loop with pow | O(d) | O(1) | Better space efficiency and beginner-friendly |
| Recursive power sum | O(d) | O(d) | Elegant but uses call stack |
toString().map to easily work with each digit of a number in Kotlin.pow causes errors because pow works on Double, not Int.