0
0
KotlinProgramBeginner · 2 min read

Kotlin Program to Check Armstrong Number

You can check an Armstrong number in Kotlin by calculating the sum of each digit raised to the power of the number of digits and comparing it to the original number using code like val sum = digits.map { it.toDouble().pow(n) }.sum().toInt() and then if (sum == number) println("Armstrong number") else println("Not Armstrong").
📋

Examples

Input153
Output153 is an Armstrong number
Input9474
Output9474 is an Armstrong number
Input123
Output123 is not an Armstrong number
🧠

How to Think About It

To check if a number is an Armstrong number, first find how many digits it has. Then, for each digit, raise it to the power of the total number of digits and add all these values together. If the sum equals the original number, it is an Armstrong number; otherwise, it is not.
📐

Algorithm

1
Get the input number.
2
Count the number of digits in the number.
3
For each digit in the number, raise it to the power of the digit count and add to a sum.
4
Compare the sum with the original number.
5
If they are equal, return that it is an Armstrong number; otherwise, return it is not.
💻

Code

kotlin
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")
    }
}
Output
Enter a number: 153 153 is an Armstrong number
🔍

Dry Run

Let's trace the number 153 through the code to see how it checks for Armstrong number.

1

Input number

number = 153

2

Extract digits

digits = [1, 5, 3]

3

Count digits

n = 3

4

Calculate sum of powers

sum = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153

5

Compare sum with original

sum == number → 153 == 153 → true

6

Print result

"153 is an Armstrong number"

DigitDigit^3
11
5125
327
💡

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

Using a loop instead of map
kotlin
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")
}
This approach uses a while loop and integer math, which may be easier to understand for beginners and avoids creating a list.
Using recursion to calculate power sum
kotlin
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")
}
This recursive method is elegant but may be less efficient for very large numbers.

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.

ApproachTimeSpaceBest For
Map with powO(d)O(d)Readable and concise code
Loop with powO(d)O(1)Better space efficiency and beginner-friendly
Recursive power sumO(d)O(d)Elegant but uses call stack
💡
Use toString().map to easily work with each digit of a number in Kotlin.
⚠️
Forgetting to convert digits to Double before using pow causes errors because pow works on Double, not Int.