skip to Main Content

im absolutely new to development, and trying to learn swift.

Right now i know how to make random number, and my next step is:
Im trying to understand how to check if my random number (127) could be divided by 2 without decimals ?
I have no idea how to do it.

2

Answers


  1. You can use operator % – remainder operator

    example:

    if randomNumber % 2 == 0 {
      print("(randomNumber) is even")
    } else {
      print("(randomNumber) is odd")
    }
    
    Login or Signup to reply.
  2. There is a specific API isMultiple(of:) in Standard Library for this purpose

    let random = Int.random(in: 0..<100)
    let isEven = random.isMultiple(of: 2)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search