GAME: Is It Prime?

 

//

//  ViewController.swift

//  test

//

//  Created by Maribel Aristy on 12/19/17.

//  Copyright © 2017 Maribel Aristy. All rights reserved.

//

import UIKit

class ViewController: UIViewController {

    @IBOutlet var numberTextField: UITextField!
    @IBAction func isItPrime(_ sender: AnyObject) {
        if let userEnteredString = numberTextField.text {
            let userEnteredInteger = Int(userEnteredString)

            if let number = userEnteredInteger {

                var isItPrime = true

                if number == 1 {

                    isItPrime = false

                }

                var a = 2

                while a < number {

                    if number % a == 0 {

                        isItPrime = false

                    }

                    a += 1

                }

                if isItPrime {

                    resultLabel.text = "\(number) is prime."

                } else {

                    resultLabel.text = "\(number) is not prime."

                }


            } else {

                resultLabel.text = "Please enter a positive whole number"

            }


        }

    }

    @IBOutlet weak var resultLabel: UILabel!

    override func viewDidLoad() {

        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.

    }

    override func didReceiveMemoryWarning() {

        super.didReceiveMemoryWarning()

        // Dispose of any resources that can be recreated.

    }

}


Happy coding! :)