Ruby Array Exercises: Check whether the sum of all the 3's of an given array of integers is exactly 9
Ruby Array: Exercise-33 with Solution
Write a Ruby program to check whether the sum of all the 3's of an given array of integers is exactly 9.
Ruby Code:
def check_array(nums)
sum = 0
i = 0
while i < nums.length
if(nums[i] == 3)
sum += 3
end
i += 1
end
return (sum == 9);
end
print check_array([3, 5, 3, 3]),"\n"
print check_array([3, 3, 2, 1]),"\n"
print check_array([3, 3, 3, 3]),"\n"
Output:
true false false
Flowchart:
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Ruby program to compute the sum of the numbers of an given array except the number 17 and numbers that come immediately after a 17. Return 0 for an empty array.
Next: 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.
What is the difficulty level of this exercise?
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-33.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics