 # How to write a function in Swift that prints prime #s

I am super new to programming and I am still struggling with prime number problem. I would appreciate some guidance.

How can I create a function that prints prime numbers .

Thank you!

Welcome back @jtalavera714 !

Here you go. Just update the end number and if you want the non-prime number, un-comment that line.

Blessings,
—Mark

``````func isPrime() {

let numbers = 1...100
for number in numbers {
if (number % 2 == 0) {
//               print("\(number)'s not prime")
} else if (number % 3 == 0) {
//               print("\(number)'s not prime")
} else if (number % 4 == 0) {
//               print("\(number)'s not prime")
} else if (number % 5 == 0) {
//               print("\(number)'s not prime")
} else if (number % 6 == 0) {
//               print("\(number)'s not prime")
} else if (number % 7 == 0) {
//               print("\(number)'s not prime")
} else if (number % 8 == 0) {
//               print("\(number)'s not prime")
} else if (number % 9 == 0) {
//               print("\(number)'s not prime")
} else {
print("\(number) IS prime" )
}
}
}

isPrime()

``````

If you want to check a single number you can do something like this:

``````func isItPrime(_ n: Int) {

var isPrime = true

if n == 1 {
isPrime = false
}
if n != 2 && n != 1 {

for i in 2 ..< n {

if n % i == 0{

isPrime = false
}
}
}

if isPrime {
print("\(n) is prime!")
} else {
print("\(n) is not prime!")
}
}

print(isItPrime(2))

``````

Blessings,
—Mark

I appreciate the response . Thank you very much. This really breaks it down for me.