Is Armstrong Number
This algorithm checks whether a given number is armstrong or not. Armstrong number is a number which is equal to sum of digits raise to the power total number of digits in the number.
Some examples of armstrong numbers are:
7 = 7^1
371 = 3^3 + 7^3 + 1^3 (27 + 343 +1)
8208 = 8^4 + 2^4 +0^4 + 8^4 (4096 + 16 + 0 + 4096)
Private Shared Function Power(x As Integer, y As Integer) As Long
Dim p As Long = 1
For i As Integer = 1 To y
p *= x
Next
Return p
End Function
Public Shared Function IsArmstrong(num As Long) As Boolean
Dim sum As Long = 0
Dim remainder As Integer
Dim digits As Integer = 0
Dim temp As Long = num
While temp <> 0
digits += 1
temp = temp \ 10
End While
temp = num
While temp <> 0
remainder = temp Mod 10
sum += Power(remainder, digits)
temp = temp \ 10
End While
Return num = sum
End Function
Example
Dim isArmstrongNum As Boolean = IsArmstrong(371)
Output
True