Back to JavaScript
2026-04-075 min read

Kotlin Program to Display Armstrong Numbers Between Intervals Using Function

Learn Kotlin Program to Display Armstrong Numbers Between Intervals Using Function step by step with clear examples and exercises.

Why This Matters

Understanding how to find patterns in numbers, such as Armstrong numbers, is essential for solving complex problems in programming interviews. By learning to write programs that can identify these patterns, you will not only impress interviewers but also develop a strong foundation in number theory and algorithmic problem-solving. Additionally, mastering the concept of Armstrong numbers can help you understand other related concepts like palindromes and perfect numbers.

Prerequisites

To fully understand this lesson, you should have a good grasp of the following concepts:

  • Basic Kotlin syntax (variables, functions, loops)
  • Number manipulation in Kotlin (calculating cube of a number)
  • Understanding of control structures like if and while statements
  • Familiarity with recursion (for implementing a recursive version of the isArmstrong() function)

Core Concept

An Armstrong number is a number that equals the sum of its cubed digits. For example, the number 153 is an Armstrong number because:

(1^3) + (5^3) + (3^3) = 1 + 125 + 27 = 153

To find all Armstrong numbers between two given integers, we can create a function called isArmstrong(). This function takes an integer as an argument and returns true if the number is an Armstrong number and false otherwise. Here's how we can implement this function:

fun isArmstrong(num: Int): Boolean {
var originalNumber = num
var digits = 0
var result = 0

// Calculate the number of digits in the given number
while (originalNumber != 0) {
originalNumber /= 10
++digits
}

// If the number has less than 3 digits, it's not an Armstrong number
if (digits < 3) return false

originalNumber = num

// Calculate the sum of cubed digits while iterating through each digit
var currentDigit: Int
var power: Double
while (originalNumber != 0) {
currentDigit = originalNumber % 10
power = Math.pow(currentDigit.toDouble(), digits.toDouble())
result += power.toInt()
originalNumber /= 10
}

// Check if the sum of cubed digits equals the original number
return result == num
}

In this implementation, we first calculate the number of digits in the given number using a while loop. Then, we initialize a variable result to store the sum of cubed digits. We iterate through each digit of the number, calculating the cube of the current digit and adding it to the result. Finally, we check if the sum of cubed digits equals the original number.

Worked Example

To display all Armstrong numbers between two given integers, we can use a loop and call our isArmstrong() function for each number. Here's an example:

fun main(args: Array<String>) {
val low = 95
val high = 999

println("Armstrong numbers between $low and $high are:")

for (number in low + 1..high - 1) {
if (isArmstrong(number)) println(number)
}
}

In this example, we're finding Armstrong numbers between 95 and 999. When you run the program, it will output:

Armstrong numbers between 95 and 999 are:
153
370
371
407

Common Mistakes

Forgetting to check if a number has less than 3 digits

When implementing the isArmstrong() function, some programmers may forget to add a check for numbers with less than three digits. This is important because these numbers cannot be Armstrong numbers due to their insufficient number of digits. To avoid this mistake, always include a check at the beginning of your implementation to ensure that the given number has at least 3 digits.

Miscalculating the sum of cubed digits

Another common mistake is miscalculating the sum of cubed digits. When calculating the sum, make sure you use the correct exponent (3) and convert the result back to an integer after calculation. Additionally, be mindful of edge cases such as single-digit numbers and numbers with leading zeros.

Not handling negative numbers correctly

When working with a function that should only handle positive integers, it's important to include a check for negative numbers at the beginning of your implementation. In this case, you can simply return false if the input is negative.

Practice Questions

  1. Write a program that finds all Armstrong numbers between 100 and 5000.
  2. Modify the isArmstrong() function to handle negative numbers correctly.
  3. Implement a recursive version of the isArmstrong() function.
  4. Write a program that generates all Armstrong numbers up to a given limit (e.g., 10,000).
  5. Modify the example program to find Armstrong numbers between two user-inputted integers.
  6. Implement an optimized version of the isArmstrong() function by calculating the sum of cubed digits in reverse order (from right to left). This optimization can help reduce the number of multiplications and divisions required.
  7. Write a program that finds all Armstrong numbers with a given number of digits (e.g., 4-digit Armstrong numbers).
  8. Implement a function to find the smallest Armstrong number with a given number of digits.
  9. Write a program that finds all Armstrong numbers within a given range that are multiples of a specific number (e.g., find all Armstrong numbers between 100 and 500 that are multiples of 3).
  10. Implement a function to find the largest Armstrong number with a given number of digits.

FAQ

Why do we need to check if a number has less than 3 digits?

Numbers with fewer than three digits cannot be Armstrong numbers because they don't have enough digits to form a sum equal to themselves when cubed. For example, the number 1 doesn't have any digits, so it can't be an Armstrong number.

Can we find Armstrong numbers using a single line of code?

While it is possible to write a one-liner that finds all Armstrong numbers between two integers, it would be difficult to read and understand. Writing clear and well-structured programs is more important in programming interviews. However, for the sake of curiosity, here's a one-liner implementation using Kotlin:

(999..10000).filter { it == (0 until it.toString().length).map { Math.pow(it % 10.toDouble(), it.toString().length.toDouble())}.sum() }
Kotlin Program to Display Armstrong Numbers Between Intervals Using Function | JavaScript | XQA Learn