# Swift Program to find the given number is strong or not

A strong number is a special number in which the sum of the factorial of its digit is equal to the number itself. For example −

Number = 345

345! = 3! + 4! + 5! = 6 + 24 + 120 = 150

Here 345 is not a strong number because the sum of the factorial of its digit is not equal to the number itself.

Number = 145

145! = 1! + 4! + 5! = 1 + 24 + 120 = 145

Here 145 is a strong number because the sum of the factorial of its digit is equal to the number itself.

In this article, we will learn how to write a swift program to find the given number is strong or not.

## Algorithm

• Step 1 − Create a function.

• Step 2 − Create a copy of the original number and initialize sum = 0.

• Step 3 − Run while to find the last digit of the given number.

• Step 4 − Now find the factorial of all the individual digits of the given number.

• Step 5 − Add the factorial of the digits and store the result in the sum variable.

• Step 6 − Compare the sum and the original number. Return true if the sum and original number are equal. Otherwise, return false.

• Step 7 − Create a test variable to store the number and pass it to the function.

• Step 8 − Print the output.

## Example

Following the Swift program to find whether the given number is strong or not.

import Foundation
import Glibc

// Function to check if te given number is strong or not
func CheckStrongNumber(num: Int) -> Bool {
var originalNum = num
var sum = 0

// Finding the sum of the factorial
// of the digits of the given number
while originalNum > 0 {

// Calculating the factorial of the digit
let lDigit = originalNum % 10
var factorial = 1
for x in 1...lDigit {
factorial *= x
}

// Calculating the sum of the factorial
// of the digits of the given number
sum += factorial
originalNum /= 10
}

// Return true if the sum and the number is equal
// Otherwise return false
return sum == num
}

// Test case 1
let myInput = 345
if CheckStrongNumber(num: myInput) {
print("YES! \(myInput) is a strong number.")
} else {
print("NO! \(myInput) is not a strong number.")
}

// Test case 2
let myInput2 = 145
if CheckStrongNumber(num: myInput2) {
print("YES! \(myInput2) is a strong number.")
} else {
print("NO! \(myInput2) is not a strong number.")
}


## Output

NO! 345 is not a strong number.
YES! 145 is a strong number.


## Conclusion

Here in the above code, we have two numbers that are 345 and 145. Now we create a function to check if the given numbers are strong numbers or not. In this function, first, we first make a copy of the original number and initialize the sum to 0. Now we run a while loop to repeatedly extract the last of the number and then perform the factorial of that digit and add the factorial result to the sum. Then the function checks if the given number is equal to the sum of the factorial of the digits. If yes then the function returns true. Otherwise, return false.

So this is how we can check if the given number is a strong number or not.

Updated on: 08-Feb-2023

121 Views 