Q:

Write a Ruby program to check three given integers x, y, z and return true if one of y or z is close (differing from a by at most 1)

0

Write a Ruby program to check three given integers x, y, z and return true if one of y or z is close (differing from a by at most 1), while the other is far, differing from both other values by 3 or more.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

def check_num(x, y, z)
  if (y-z).abs < 3
        return false
    end
    return (x-y).abs<=1 && (x-z).abs>=3 || (x-z).abs<=1 && (x-y).abs>=3
end
print check_num(2, 3, 11),"\n"
print check_num(2, 3, 4),"\n"
print check_num(5, 2, 4)

Output:

true
false
false

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now