Ruby Array Exercises: Compute the sum of all the elements
Ruby Array: Exercise-9 with Solution
Write a Ruby program to compute the sum of all the elements. The array length must be 3 or more.
Ruby Code:
def check_array(nums)
return (nums[0] + nums[1] + nums[2])
end
print check_array([1, 2, 5]),"\n"
print check_array([1, 2, 3]),"\n"
print check_array([1, 2, 4])
Output:
8 6 7
Flowchart:
Ruby Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Ruby program to remove blank elements from an given array.
Next: Write a Ruby program to split a delimited string into an array.
What is the difficulty level of this exercise?
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework