w3resource

Ruby Array Exercises: Check whether every element is a 3 or a 5 in a given array of integers

Ruby Array: Exercise-35 with Solution

Write a Ruby program to check whether every element is a 3 or a 5 in a given array of integers.

Ruby Array Exercises: Check whether every element is a 3 or a 5 in a given array of integers

Ruby Code:

def check_array(nums)
   i = 0
   while i < nums.length
        if(nums[i] != 3 && nums[i] != 5)
			return false
		end	
	i = i + 1
   end
   return true
end
print check_array([3, 5, 3, 3]),"\n"
print check_array([2, 3, 2, 5]),"\n"
print check_array([3, 5, 5, 5]),"\n"

Output:

true
false
true

Flowchart:

Flowchart: Check whether every element is a 3 or a 5 in an given array of integers

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to check whether the number of 2's is greater than the number of 5's of a given array of integers.
Next: Write a Ruby program to check whether it contains no 3 or it contains no 5.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/ruby-exercises/array/ruby-array-exercise-35.php