w3resource

Ruby Array Exercises: Check whether a given array of integers of length 2 contains a 4 or a 7

Ruby Array: Exercise-17 with Solution

Write a Ruby program to check whether a given array of integers of length 2 contains a 4 or a 7.

Ruby Array Exercises: Check whether a given array of integers of length 2 contains a 4 or a 7

Ruby Code:

def check_array(nums)
    if(nums[0] == 4 || nums[0] == 7)
		return true
	end	
	return (nums[1] == 4 || nums[1] == 7)
end

print check_array([1, 4]),"\n" 
print check_array([7, 5]),"\n"   
print check_array([5, 2])  

Output:

true
true
false

Flowchart:

Flowchart: Check whether a given array of integers of length 2 contains a 4 or a 7

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to concatenate array of arrays into one.
Next: Write a Ruby program to check whether a given array of integers of length 2 does not contain a 6 or a 9.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.