Ruby, Else


I have code below and when run it the result is wrong. I tested with 9 or 6 or 3 it give the wrong phrase (NOT divisible), please help me to check.

print "Input number to test divisible by three: "
number = gets.chomp
if number % 3 == 0
print "this number is divisible by three"
print "this number is NOT divisible by three"


You are working with a string.
You need to convert string to an integer before you can do mathematical expressions the way you’d expect.

number.to_i will do the trick


Its done. Thank you!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.