Check the number is prime or not in swift 3.0

//code

import UIKit

var flag=0
var n = 13

var i = 2
for i in 2 ... n-1
{
    if(n%i==0)
    {
        flag = 1;
        break;
    }
}

if(flag==1)
{
    print("\(n)" + " is not prime")
}
else
{
    print("\(n)" + " is  prime")

}


Result:



0 comments:

Post a Comment