Ruby Basic Exercises: Check two given integers, each in the range 10..99, return true if there is a digit that appears in both numbers
Ruby Basic: Exercise-51 with Solution
Write a Ruby program to check two given integers, each in the range 10..99, return true if there is a digit that appears in both numbers.
Ruby Code:
def check_num(a, b)
a_digit = a%10
b_digit = b%10;
a /= 10;
b /= 10;
return (a == b || a == b_digit || a_digit == b || a_digit == b_digit)
end
print check_num(9, 12),"\n"
print check_num(15, 51),"\n"
print check_num(12, 23)
Output:
false true true
Flowchart:
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Ruby program to check two given integers and return the larger value. However if the two values have the same remainder when divided by 5 then return the smaller value and if the two values are the same, return 0.
Next: Write a Ruby program to check three given integers and return their sum. However, If one of the values is the same as another of the values, it does not count towards the sum.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/ruby-exercises/basic/ruby-basic-exercise-51.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics