-4

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 Answers2

7

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)
vadian
  • 274,689
  • 30
  • 353
  • 361
0

You can use operator % - remainder operator

example:

if randomNumber % 2 == 0 {
  print("\(randomNumber) is even")
} else {
  print("\(randomNumber) is odd")
}
Andrew
  • 1,456
  • 15
  • 23
  • Привет) Спасибо большое что ответил. – Stanislav Konovalov Mar 19 '22 at 19:00
  • Я ввел то что ты сбросил и заменил "randomNumber" на мое число (127), но после того как нажал "Run" в консоли - ничего не произошло. – Stanislav Konovalov Mar 19 '22 at 19:01
  • 3
    @StanislavKonovalov this international version of StackOverflow by rules only English can be used. you can use https://ru.stackoverflow.com/ to discover answers on your language. Also, you can try updated code, it will print your number with info of is it even or odd. – Andrew Mar 19 '22 at 19:07