Create a Ruby program to do the following.... Within a Ruby program, have an arr
ID: 3573861 • Letter: C
Question
Create a Ruby program to do the following....
Within a Ruby program, have an array of mixed integers (with even and odd number).
Say for example: anArray = [45, 4, 1, 6, 11, 8, 2, 9, 10, 5, 13];
Then prompt the user to input a word 'odd' or 'even':
Please enter 'odd(o)' or 'even(e)' to see what is inside the array:
If the user enters 'o' or 'odd' then print the odd numbers of the array like this:
Odd numbers of array : [45, 1, 11, 9, 5, 13]
If the user enters 'e' or 'even' then print the even numbers of the array like this:
Even numbers of array : [4, 6, 8, 2, 10]
Explanation / Answer
# Input numbers
nums = Array[45, 4, 1, 6, 11, 8, 2, 9, 10, 5, 13]
# Taking input from the user and trimming new line
operation = gets
operation = operation.chomp
# Logic to remove numbers from array based on given operation
if operation == "e" or operation == "even"
nums = nums.delete_if{|i|i%2 == 1}
puts "Even numbers array : [" + nums.join(",") +"]"
elsif operation == "o" or operation == "odd"
nums.delete_if{|i|i%2 == 0}
puts "Odd numbers array : [" + nums.join(",") +"]"
else
puts "Invalid operation!!"
end