# Kotlin Program to Print Armstrong Number Between Interval

Write a Kotlin Program to Print Armstrong Number Between Interval a and b, given a < b.

### What is Armstrong Number ?

A positive integer (x) of n digits is called armstrong number of order n (number of digits in x) if

```abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + ....
```

Note that abcd… is number x where a, b, c, d ….etc. are it’s digits. n is number of digits in x.

For example,

```Input:
a = 1 and b = 5

Output:
1 2 3 4 5
```
```Input:
a = 6 and b = 200

Output:
6 7 8 9 153
```

## 1. Program to Print Armstrong Number Between Interval

Sourcecode –

```import java.util.*
import kotlin.math.floor
import kotlin.math.log10
import kotlin.math.pow
import kotlin.math.roundToInt

fun main() {
val read = Scanner(System.`in`)

println("Enter a:")
val a = read.nextInt()

println("Enter b:")
val b = read.nextInt()

if(a >= b) {
println("Invalid interval")
return
}

println("Armstrong number between \$a and \$b: ")
for(i in a..b) {
if(checkArmstrong((i))) {
print("\$i ")
}
}
}

private fun checkArmstrong(number: Int): Boolean {

// Get the count of digits in number
val n = floor(log10(number.toDouble()) + 1).roundToInt()

var x = number

// Find sum by power last digit in n to count.
var sum = 0
while(x > 0) {
val digit = x % 10
sum = (sum + (digit.toDouble().pow(n))).roundToInt()
x /= 10
}

// Return true if sum is equal to number
return sum == number
}
```

When you run the program, output will be –

```Enter a:
1
Enter b:
5000
Armstrong number between 1 and 5000:
1 2 3 4 5 6 7 8 9 153 370 371 407 1634
```
###### Explanation:

Here, we have created an object of Scanner. Scanner takes an argument which says where to take input from.
System.`in` means take input from standard input – Keyboard.

read.nextIn() means read anything entered by user before space or line break from standard input – Keyboard.

Read the interval (a and b) entered by user and store it in variable a and b.

Then,
Run a loop from a to b.
Check for each value (i) if it is an armstrong number. If yes, print the value.

Thus, we went through Kotlin Program to Print Armstrong Number between interval a and b.