Check the given number is perfect number or not in swift 3.0


Code:

import UIKit

var n = 28
var temp = n
var sum = 0

for i in 1 ... n-1
{
    if(n%i==0)
    {
        sum = sum + i
    }
}
if(sum == temp)
{
    print("\(temp) is perfect number.")
}
else
{
     print("\(temp) is not perfect number.")

}


Result:


0 comments:

Post a Comment