Ruby整数甚至?示例函数
偶? Ruby 中的函数返回一个布尔值。如果数字是偶数则返回true,否则返回false。
Syntax: number.even?
Parameter: The function takes the integer which is to be checked for even or not.
Return Value: The function returns a boolean value which determines if the value is even or not.
示例 #1:
# Ruby program of Integer even? function
# Initializing the numbers
num1 = 19
num2 = 2
num3 = 28
num4 = 13
# Prints the integer division
# and its remainder
puts num1.even?
puts num2.even?
puts num3.even?
puts num4.even?
输出:
false
true
true
false
示例 #2:
# Ruby program of Integer even? function
# Initializing the numbers
num1 = 18
num2 = 200
num3 = 2987
num4 = 13
# Prints the integer division
# and its remainder
puts num1.even?
puts num2.even?
puts num3.even?
puts num4.even?
输出:
true
true
false
false