Ruby, Else


#1

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"
else
print "this number is NOT divisible by three"
end


#2

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


#3

Its done. Thank you!


#4

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