# Swift Program to Check Armstrong Number

This tutorial will discuss how to write swift program to check Armstrong number.

The sum of nth power of individual digit of a number is equal to the number itself, then such type of number is known as Armstrong number.

Suppose we have a number 407 so here n = 3

153 = 13 + 53 + 33

153 = 1 + 125 + 27

153 = 153

Hence 153 is Armstrong number

Suppose we have another number 8973, so here n = 4

8973 = 84 + 94 + 74 + 34

8973 = 4096 + 6561 + 2401 + 27

8973 = 13085

Hence 8973 is not an Armstrong number.

Below is a demonstration of the same −

Input 1

Suppose our given input is −

Number = 407


Output

The desired output would be −

YES! 407 is an Armstrong Numbers


Input 2

Suppose our given input is −

Number = 12489


Output

The desired output would be −

NO! 12489 is not an Armstrong Numbers


### Algorithm

Following is the algorithm −

• Step 1 − Declare two variables to store the lower and upper limit.

• Step 2 − Run a for loop from lower limit to upper limit to iterate through each element.

• Step 3 − Declare another variable to store the sum.

• Step 4 − Declare on more variable to store the count of the total number of digits present in the given number.

• Step 5 − Count the total number of digits present in the given number −

while(armNum != 0){
armNum = armNum/10
count = count + 1
}

• Step 6 − Calculate the sum of power of individual digit the given number

let rem = armNum % 10
sum = sum + (rem * rem * rem)
armNum = armNum/10

• Step 7 − Compare the sum with the number itself. If both sum and number are equal, then the number is the Armstrong number. Otherwise not.

• Step 8 − Print the output.

## Check 3-digit Armstrong Numbers

### Example

The following program shows how to check an Armstrong number.

import Foundation
import Glibc

var myNumber = 407
var sum = 0
var n = myNumber

// Calculate the sum of 3rd power
// of individual digit the given number
while (n != 0){
let rem = n % 10
sum = sum + (rem * rem * rem)
n = n/10
}

// If the sum is equal to the given number
// Then the number is Armstrong number
if (sum == myNumber){
print("YES! \(myNumber) is an Armstrong number")
}
else{
print("NO! \(myNumber) is not an Armstrong number")
}


### Output

YES! 407 is an Armstrong number


Here, in the above code, we have a number = 407. Now we find the sum of 3rd power of the individual digit of the number. After calculating the sum now we find compare the sum is equal to the given number. If the sum is equal to the number, then the number is Armstrong number. Otherwise not. So the working of the above code is −

myNumber = 407
Sum = 0
1st iteration:
while (407 != 0){
let rem = 407 % 10 = 7
sum = sum + (rem * rem * rem) = 0 + (7 * 7 * 7) = 343
n = 407/10 = 40
}

2nd iteration:
while (40 != 0){
let rem = 40 % 10 = 0
sum = sum + (rem * rem * rem) = 343 + (0* 0 * 0) = 343
n = 40/10 = 4
}

3rd iteration:
while (4 != 0){
let rem = 4 % 10 = 4
sum = sum + (rem * rem * rem) = 343 + (4* 4 * 4) = 343 + 64 = 407
n = 4/10 = 0
}
Sum = 407
if (407 == 407) // Condition is true{
print("YES! \(myNumber) is an Armstrong number")
}


Hence the output is “YES! 407 is an Armstrong number” because 407 is as Armstrong number.

## Check n-digit Armstrong Numbers

### Example

The following program shows how to check an Armstrong number.

import Foundation
import Glibc

var num = 40689

print("Number:", num)

var sum = 0
var armNum = num
var count = 0

// Count the total number of digits
// present in the given number
while(armNum != 0){
armNum = armNum/10
count = count + 1
}

armNum = num

// Calculate the sum of nth power
// of individual digits of the given number
while (armNum != 0){
let rem = armNum % 10
sum = sum + Int(pow(Double(rem), Double(count)))
armNum = armNum/10
}

// If the sum is equal to the given number
// Then the number is Armstrong number
if (sum == num){
print("Yes the number is an Armstrong number")
}
else{
print("No the number is not an Armstrong number")
}


### Output

Number: 40689
No the number is not an Armstrong number


Here, in the above code, we have a number = 40689. Now we check the number is an Armstrong number or not. So, to find Armstrong number, we first calculate the total number digits present in the number. After that we find the sum of nth power of the individual digit of the number. Now after calculating the sum we check the sum is equal to the given number or not by comparing each other. If the sum is equal to the number, then the number is Armstrong number and print the output. So number = 40689 is not an Armstrong number.

Updated on: 20-Oct-2022

705 Views