Ruby program to take a number as input and tells its even or odd number

Views: 20   

Following program shows you how to take a number as input and tells its even or odd number.
This program divides input by 2, If remainder is zero it prints even otherwise it prints odd

 puts "Please enter a number:"
 input = Integer(gets.chomp)
 if input % 2 == 0
 puts "even"
 else
 puts "odd"
 end

Output:

Example1:

Please enter a number:
4
even

Example2:

Please enter a number:
-5
odd
On By