How to check if the given number is Armstrong ?

[Question]: Check if given number is Armstrong. for example : 371 =  3 pow 3 + 7 pow 3 + 1 pow 3 = 371 The count of digits to power of each digits & sum is equivalent to self number. (pow id power )

func checkArmStrong(n: inout Int) -> Bool {
    var count = 0
    var firstCopy = n
    var originalCopy = n
    var sumOfPower = 0
    
    while firstCopy != 0 {
        count+=1
        firstCopy = firstCopy/10
    }
    
    while n != 0 {
        let digit = n%10
        sumOfPower += Int(pow(Double(digit), Double(count)))
        n = n/10
    }
    return sumOfPower == originalCopy
    
}

var ipNumber = 153
print(checkArmStrong(n: &ipNumber))// true.

Leave a Comment

Your email address will not be published. Required fields are marked *