How to Display Armstrong Numbers Between Intervals Using Function in Kotlin

Armstrong numbers are a special type of number that is equal to the sum of the cubes of its digits.

In this tutorial, we will discuss how to display Armstrong numbers between intervals using a function in Kotlin.

To start, let’s first define what an Armstrong number is in programming terms.

An Armstrong number is a number that satisfies the following condition:

If we take the sum of the cubes of its digits, it will be equal to the number itself.

For example, 153 is an Armstrong number because:

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

Now, let’s move on to the code. We will first create a function that takes in two integers, the lower and upper bounds of the interval.

The function will then iterate through each number in the interval and check if it is an Armstrong number.

Here’s the code:

fun displayArmstrongNumbers(lower: Int, upper: Int) {
    for (i in lower..upper) {
        var num = i
        var sum = 0
        var power = 0
        
        // Find the sum of cubes of digits
        while (num > 0) {
            val digit = num % 10
            sum += digit.toDouble().pow(3).toInt()
            num /= 10
        }
        
        // Check if the number is Armstrong
        if (sum == i) {
            println(i)
        }
    }
}

Let’s break down the code. The displayArmstrongNumbers function takes in two parameters, lower and upper, which represent the lower and upper bounds of the interval, respectively.

We then iterate through each number in the interval using a for loop, starting from lower and ending at upper.

For each number i, we first create a variable num and set it to i.

We also create a variable sum and set it to 0, which will be used to calculate the sum of the cubes of its digits.

We also create a variable power and set it to 0, which will be used to calculate the cube of each digit.

We then enter a while loop that continues until num is greater than 0.

In each iteration of the loop, we find the last digit of num using the modulo operator (num % 10), and add the cube of the digit to sum using the pow function from the kotlin.math package.

After finding the sum of the cubes of digits, we check if it is equal to the number i. If it is, we print the number i to the console.

That’s it! Now we can call the displayArmstrongNumbers function with our desired interval to display all Armstrong numbers between those intervals.

Here’s an example:

fun main() {
displayArmstrongNumbers(100, 1000)
}

This will display all Armstrong numbers between 100 and 1000.

In conclusion, displaying Armstrong numbers between intervals is a straightforward process in Kotlin.

We can create a function that takes in the lower and upper bounds of the interval, iterate through each number in the interval, and check if it is an Armstrong number using the sum of the cubes of its digits.

We hope this post has been helpful in understanding how to display Armstrong numbers using a function in Kotlin.