# Kotlin Program to Check if Number is Armstrong Number

###### Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.

Write a Kotlin Program to Check A Number is Armstrong number or not

### 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:
120

Output:
No, 120 is not armstrong number.
```
```Input:
423432

Output:
No, 423432 is not armstrong number.
```
```Input:
153

Output:
Yes, 153 is armstrong number.
```

## 1. Program to Check Armstrong Number

Sourcecode –

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

fun main() {

println("Enter x:")

if(checkArmstrong(x)) {
println("Yes, \$x is armstrong number.")
} else {
println("No, \$x is not armstrong number.")
}
}

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 n:
1634
Yes, 1634 is armstrong number.
```
###### 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.

Stored the value read using nextInt() in variable x

We defined 1 additional functional –

• checkArmstrong: returns true if number passed as argument is Armstrong Number. Else, returns false.

Inside checkArmstrong function,

• Count number of digits and store in variable n
• Then, For every digit r in number x, compute r^n

For example,

For x = 153
n = 3.

Now, run a while loop till x != 0

For x = 153,
digit = x % 10 => digit = 153 % 10 => digit = 3
sum = (sum + (digit.pow(n))) => sum = 0 + 3^3 => sum = 27
x /= 10 => x = 15

For x = 15,
digit = x % 10 => digit = 15 % 10 => digit = 5
sum = (sum + (digit.pow(n))) => sum = 27 + 5^3 => sum = 27 + 125 => sum = 152
x /= 10 => x = 1

For x = 1,
digit = x % 10 => digit = 1 % 10 => digit = 1
sum = (sum + (digit.pow(n))) => sum = 152 + 1^3 => sum = 152 + 1 => sum = 153
x /= 10 => x = 0

At x = 0 , condition 0 != 0 is false.
So, while loop is terminated.

Thus, we went through Kotlin Program to check if given number is armstrong number or not.