w3resource

Ruby Basic Exercises: Check two non-negative integer values and return true if they have the same last digit

Ruby Basic: Exercise-30 with Solution

Write a Ruby program to check two non-negative integer values and return true if they have the same last digit.

Sample array : ["Ruby", 2.3, Time.now]

Ruby Code:

def test_last_digit(a, b)
   return (a % 10 == b % 10);
end
print test_last_digit(5, 5),"\n"
print test_last_digit(15, 25),"\n"
print test_last_digit(256, 346),"\n"
print test_last_digit(26, 34)

Output:

true
true
true
false

Flowchart:

Flowchart: Check two non-negative integer values and return true if they have the same last digit

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to print the elements of a given array.
Next: Write a Ruby program to retrieve the total marks where subject name and marks of a student stored in a hash.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.